All events are run on the thread that raises the event. In the case of System.Timers.Timer, its event is raised from a thread pool thread. If you specify a SynchronizingObject value (which is specified when you drag it onto a form in the designer), then the thread pool thread invokes the message on the synchronizing object (which would invoke on the UI thread).
If you create one through code and don't specify a SynchronizingObject value, then it activates on a thread pool thread. Or from the docs: "When SynchronizingObject is a null reference (Nothing in Visual Basic), the method that handles the Elapsed event is called on a thread from the system-thread pool. For more information on system-thread pools, see ThreadPool. When the Elapsed event is handled by a visual Windows Forms component, such as a button, accessing the component through the system-thread pool might result in an exception or just might not work. Avoid this effect by setting SynchronizingObject to a Windows Forms component, which causes the method that handles the Elapsed event to be called on the same thread that the component was created on." Adam.. > -----Original Message----- > From: Unmoderated discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED] On Behalf Of > Stephen L. Bub > Sent: Wednesday, September 07, 2005 4:00 PM > To: [email protected] > Subject: [ADVANCED-DOTNET] Multi threading and events > > Hi, > > My name is Stephen. Could you please tell me whether events > are fired using the thread that raised the event, a new > thread, or a thread from the thread pool. For example, what > thread does a System.Timers.Timer Elapsed event run on. How > can I find this out for each class, etc. I am developing an > application using multiple serial communications ports and > timers, and this would help me determine whether I need to > use multi- threading. If new or pooled threads are used to > service events, then why would I need to use multi-threading? > Currently, when I call Thread.Sleep > (10000) on the thread in the user interface, the rest of the > application in seperate classes, using timers and > communication ports, keeps running. =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
