I have few controls on my MainForm in Winforms application. For example control that updates progress of each operation. These operations are classes which in run in different Thread.
How can i properly update those controls ?
I have few controls on my MainForm in Winforms application. For example control that updates progress of each operation. These operations are classes which in run in different Thread.
How can i properly update those controls ?
the best way is to do that by events. the easier way is to change them directly.
ensure that they are public and you overgive them to the class and then you can change it
Invoke(new MethodInvoker(delegate { frmMain.label1.Text = "bla"; }));
in your main form you can add a function like this one
private delegate void doSomethingWithTheControlsDelegate(object obj);
public void doSomethingWithTheControls(object obj) {
if (this.InvokeRequired) {
this.BeginInvoke(new doSomethingWithTheControlsDelegate(this.doSomethingWithTheControls), obj);
} else {
// do something
}
}
I would suggest using a model class which would contain the data which is displayed to the user. Databind your UI controls to the properties of the model, and update the model values from the worker threads (using appropriate invokations to ensure the update occurs on the UI thread so that you don't get the cross thread exception)