Updating UI from another thread in C#
I have a silly question which prevents me to proceed with my project. My project involves multi-threading(foreground threads), so i would like to have all my threads kept in a different .cs file than Form1.cs.
Is this a good call or i should write all my methods in the Form.cs? All the examples found on the net have the methods开发者_如何学编程 in the same class.
The problem that I am facing is: how to update my controls in the Form1. If i had put my methods in the Form class it would be easy, i would have used:
if(this.InvokeRequired)
this.Invoke((Action)(() => richTextBox.Text += (line + Environment.NewLine)));
but updating that control from another class it gives me a headache, knowing that i might miss something rather simple.
i would like to have all my threads kept in a different .cs file than Form1.cs.
This statement is somewhat inaccurate as threads simply have execution contexts, which can be code from your Form1.cs or any other class file. However, I always prefer to only have form logic/manipulation code in my forms. Any other code should be elsewhere. With that, your other classes should NOT know about your form - that produces unnecessary coupling.
Use events or callbacks to invoke responses you want of your form. Your form should then contain the code that invokes your UI manipulation code to run on the main UI thread.
Is this a good call or i should write all my methods in the Form.cs?
There are lots of good reasons to separate the application logic from the GUI. For example, if you want to write two different applications that share some of the same logic then you can put this logic in its own class. It also helps separate components by responsibility (calculation vs user interaction) which can make your design easier to understand and to test. Your approach is reasonable, although it does mean you have a little more work to do.
The problem that I am facing is: how to update my controls in the Form1.
You can add events to your new class and fire them when you want to notify some progress or information. In your main form you can subscribe to these events and call the appropriate methods to update the GUI (remembering to call Invoke if needed).
Keep all UI manipulation in Form.cs like:
public void UpdateText(string text)
{
if (InvokeRequired) Invoke(() = UpdateText(text));
else myLabel.Text = text;
}
In other classes just keep reference to your form, so you can call methods on in.
The code to actually update the controls should be in your Form.cs file. The exact way that you are performing the update will depend upon how you want to invoke the update. I'd recommend looking at the Model-View-Controller pattern. You want to separate you business logic for operations from your form but your form should encapsulate the controls and logic for updating those controls.
If your methods require updating the controls of the form, then of course go ahead and put them in the form class. I am expecting this to be the case here.
You can also use callback.. so you can seperate you logic from your UI
精彩评论