[.net] multithreaded gui with C# question
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
In WinForms only the thread that created a control can access that control. All other threads needs to go indirectly using BeginInvoke.
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.
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.
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 }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement