Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

Accessesing GUI from different thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve N. - 14 Jul 2005 21:06 GMT
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
Tomas Restrepo (MVP) - 15 Jul 2005 00:57 GMT
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

Signature

Tomas Restrepo
tomasr@mvps.org
http://www.winterdom.com/

Steve N. - 25 Jul 2005 17:11 GMT
> 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
[quoted text clipped - 4 lines]
> same):
> http://www.code-magazine.com/article.aspx?quickid=0403071&page=1

Yes, I got it working, thank you.
Arnaud Debaene - 16 Jul 2005 17:47 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.