Sign in to follow this  
cpp_boy

[.net] multithreaded gui with C# question

Recommended Posts

cpp_boy    100
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

Share this post


Link to post
Share on other sites
cpp_boy    100
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.

Share this post


Link to post
Share on other sites
paulecoyote    1065
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.

Share this post


Link to post
Share on other sites
Arild Fines    968
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
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this