Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am not familiar with threading.
I have tried googling but none of the suggestions I found work correctly.
I have an application that calculates the the shortest routes given points on the form.
The shortest route is draw after each iteration by connecting the points from start to finish.

this.workerThread = new Thread(new ThreadStart(this.DrawRoute));<br />
              this.workerThread.Start();<br />
              while (workerThread.IsAlive)<br />
              {<br />
<br />
                  Application.DoEvents();<br />
<br />
              }


This is the code I have which works, but by calling the Application.DoEvents(), I am unable to update the form while it is running. ie. update a label with the new shortest route.
Is there another way of doing it? I have tried using delegates but that did not seem to work, and if I just use workerThread.Start(); it does not work either.

Thanks for any help.

Chris
Posted

1 solution

You need to use the Invoke method to marshal calls from your worker thread back to your form.
 
Share this answer
 
Comments
Chriso82 14-Oct-10 5:12am    
Could you possibly giev me an example?
Chriso82 14-Oct-10 5:13am    
Do I have to use a delegate within the thread?
Nathan St 14-Oct-10 5:24am    
There's an example in that link. lblResult.Invoke(new MethodInvoker(delegate { lblResult.Text = result; }));
Chriso82 14-Oct-10 6:41am    
Thanks alot Nat
Nathan St 14-Oct-10 7:25am    
My pleasure :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900