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 / C# / July 2007

Tip: Looking for answers? Try searching our database.

Callback mainthread in a class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cheryl - 19 Jul 2007 11:56 GMT
Hi all.  I am writing a component library that does not have an interface.
I would like to create a class that allows the worker thread to callback the
mainthread so that the Form interface does not have to invoke the main
thread before updating the control.  Would this be possible?  If yes, how
should I do so?
Nicholas Paldino [.NET/C# MVP] - 19 Jul 2007 15:51 GMT
Cheryl,

   Are you saying that you want to have the event that you fired be on the
UI thread?  If this is the case, instead of just firing the event normally,
you could cycle through the delegate chain.  If the object that the
MethodInfo that the delegate is pointing to has an ISynchronizeInvoke
implementation, you can pass the delegate and the parameters to that.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi all.  I am writing a component library that does not have an interface.
> I would like to create a class that allows the worker thread to callback
> the mainthread so that the Form interface does not have to invoke the main
> thread before updating the control.  Would this be possible?  If yes, how
> should I do so?
Cheryl - 20 Jul 2007 02:12 GMT
Yes.  What I mean is to fire the event on the UI thread.  Sorry that I am
not too familar with the multithreading library in C#.  Would you mind
giving me more illustration on "cycle through the delegate chain"?

> Cheryl,
>
[quoted text clipped - 9 lines]
>> invoke the main thread before updating the control.  Would this be
>> possible?  If yes, how should I do so?
Nicholas Paldino [.NET/C# MVP] - 20 Jul 2007 21:16 GMT
Cheyrl,

   When you make a call to fire an event, you usually do something like
this:

eventHandler(this, new EventArgs());

   Or something similar, where eventHandler is the event declaration for
your event (of course, you check for null as well).

   Instead of that, you would do this:

foreach (EventHandler handler in eventHandler.GetInvocationList())
{
   // Check for the ISynchronizeInvoke implementation.
   ISynchronizeInvoke si = handler.Target as ISynchronizeInvoke;

   // If there is an implementation, call it on that thread.
   if (si != null)
   {
       // Call.
       si.Invoke(handler, new object[2]{this, new EventArgs()});
   }
   else
   {
       // Call normally.
       handler(this, new EventArgs);
   }
}

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Yes.  What I mean is to fire the event on the UI thread.  Sorry that I am
> not too familar with the multithreading library in C#.  Would you mind
[quoted text clipped - 13 lines]
>>> invoke the main thread before updating the control.  Would this be
>>> possible?  If yes, how should I do so?

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.