-3

Here in my code....

List<Task> t;

private void Form1_Load(object sender, EventArgs e)
{
    t = new List<Task>();
    t.Add(Task.Factory.StartNew(() => Download()));
    t.Add(Task.Factory.StartNew(() => Display()));
}

Now in display method when I hide any control it gives me a "cross thread exception" and tell that it is used by main thread.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123

2 Answers2

0

It is obvious that , Display method is running on a different thread then on which it was created.

You can only make changes to WinForm controls from the master thread. You need to check whether InvokeRequired is true on the control and then Invoke the method as needed.

To update any infomation on control.

you can try this way :

if (this.InvokeRequired)
{
    this.Invoke((MethodInvoker)delegate { update the ui control here});
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
0

You have to invoke your operations on the main thread. You need to do something like:

Invoke((Action)(() => updateProgressBar()));

This will run the updates on the main UI thread.

Pete Garafano
  • 4,863
  • 1
  • 23
  • 40