0

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 ?

Suman Banerjee
  • 1,923
  • 4
  • 24
  • 40
eugeneK
  • 10,750
  • 19
  • 66
  • 101
  • here is the link that solves your situation: http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c – Donatas K. Nov 17 '11 at 08:09

3 Answers3

1

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"; }));
masterchris_99
  • 2,683
  • 7
  • 34
  • 55
1

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
 }
}
punker76
  • 14,326
  • 5
  • 58
  • 96
0

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)

flipchart
  • 6,548
  • 4
  • 28
  • 53
  • Well, i do want to implement it but don't know how. If you have some working example in bookmarks/favorites i would love to read it. Thanks – eugeneK Nov 17 '11 at 08:17