Accessesing GUI from different thread

  • Thread starter Thread starter Steve N.
  • Start date Start date
S

Steve N.

Whats the best way to access .net GUI classes from a spawned thread? Is
this even advisable, or is it a sign that the design is wrong?

Any input, links, etc., would be helpful. Thanks!

Steve
 
Steve,
Whats the best way to access .net GUI classes from a spawned thread? Is
this even advisable, or is it a sign that the design is wrong?

It is certainly possible, and certainly necessary at times. It's not
necessarily a wrong thing, though in some cases there might be alternatives.

That said, you do have to take a few things into account, and make sure you
marshal the GUI access back to the main thread (so that you never touch the
gui controls from secondary threads), but, fortunately, the .NET framework
provides a mechanism for this.

Here's a good article on the topic (it's in C#, but the principles are the
same):
http://www.code-magazine.com/article.aspx?quickid=0403071&page=1
 
Steve said:
Whats the best way to access .net GUI classes from a spawned thread?
Is this even advisable, or is it a sign that the design is wrong?

The design is not wrong, but you jmust be careful to make your GUI API
thread-safe : each public function of your GUI objects that may be called on
different threads should begin by something like this (in C# for ease of
syntax, but you've got the idea) :

delegate void DoSomethingDelegate(params...) ///DoSomethingDelegate matches
DoSomething'signature
void DoSomething(params....)
{
if (InvokeRequired)
{
BeginInvoke (new DoSomethingDelegate(DoSomething, new object[]
{params...}));
return;
}

//normal GUI stuff
}

Arnaud
MVP - VC
 
Tomas said:
That said, you do have to take a few things into account, and make sure you
marshal the GUI access back to the main thread (so that you never touch the
gui controls from secondary threads), but, fortunately, the .NET framework
provides a mechanism for this.

Here's a good article on the topic (it's in C#, but the principles are the
same):
http://www.code-magazine.com/article.aspx?quickid=0403071&page=1

Yes, I got it working, thank you.
 
Back
Top