[.net] multithreaded gui with C# question

Started by
3 comments, last by Arild Fines 18 years, 4 months ago
Hello. I'm writing this program in wich multiple threads will share GUI with common GUI components like textboxes, buttons etc. I have no idea how those threads will share this GUI. They all have to somehow subscribe to Form events.... Can someone please show me simple example of a program with few threads sharing simple GUI
Advertisement
In WinForms only the thread that created a control can access that control. All other threads needs to go indirectly using BeginInvoke.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Quote:Original post by dalleboy
In WinForms only the thread that created a control can access that control. All other threads needs to go indirectly using BeginInvoke.



WOW. Now I'm complitely confused :-)
Simple example will be very appreciated.
In C#2 you've got a new class called "BackgroundWorker" which lets you override events and report progress... wrapping up all that invoke stuff behind the scenes.

One of the rules of win32 (.Net or otherwise) is that you shouldn't access a control on a different thread from the one that created it. If you think about it in terms of encapsulation and predictability that actually makes sense. Thing is with C++ you can actually do it anyway (though of course you really probably should not), where as C# tells you off if you try.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
I usually do something like this:

public class SomeForm{ private delegate void BlahDelegate(); public void BlahMethodCalledByOtherThread() {   if (this.InvokeRequired)   {     this.Invoke( new BlahDelegate(this.BlahMethodCalledByOtherThread), new object[]{} );     return;        }   // do stuff here }}
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement