views:

285

answers:

1

Hello,

this morning I saw some strange code from one of my coworkers. In an winforms application the whole Mainform was given into a thread as reference. He did this to invoke methods from the Form. Nothing to interact with the UI, just plain calculation methods. I am already sure this is not a best-practice but I wonder something. If I call methods from the Mainform from inside a thread are these methods still processed as multithreading? Or do they run inside the Mainform? Will the UI thread be affected when I call methods from the Mainfrom from a thread?

+5  A: 

Functions from the form called from a different thread will run on the thread that is calling them and have no effect on the GUI thread. As a side effect, this means that if you call a function that interacts with the GUI you will get an exception.

If you want them to be called on the GUI thread, the easiest thing to do is call Control.Invoke(...) which causes the delegate you pass in to be executed on the GUI thread, allowing GUI interaction.

Jon Norton