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 / Windows Forms / WinForm General / August 2006

Tip: Looking for answers? Try searching our database.

Execution context for an event handler

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mehdi_mousavi - 30 Aug 2006 13:32 GMT
Hi folks,
Consider the following code:

MyClass c = new MyClass();
c.MyEvent += new MyEventHandler(MyHandler);

where MyClass is defined as follows:

public class MyClass
{
   public delegate void MyEventHandler(object sender, EventArgs e);
   public event MyEventHandler MyEvent = null;

   //Other things go here...
}

and MyHandler is defined this way:

private void MyHandler(object sender, EventArgs e) {}

The question is that what context the "MyHandler" method is executed
on? Is it executed under the same thread where the Event Handler is
called? or does it use the "thread pool" to execute the handler on
another thread?

Any help would be highly appreciated,

Cheers,
Mehdi
Jon Shemitz - 30 Aug 2006 19:02 GMT
> c.MyEvent += new MyEventHandler(MyHandler);

> public class MyClass
> {
[quoted text clipped - 3 lines]
>     //Other things go here...
> }

> The question is that what context the "MyHandler" method is executed
> on? Is it executed under the same thread where the Event Handler is
> called? or does it use the "thread pool" to execute the handler on
> another thread?

Assuming that MyClass raises the event in the normal way

 protected void OnEventHandler(EventArgs e)
 {
   if (MyEventHandler != null)
     MyEventHandler(this, e);
 }

then the "MyHandler" method is executed in the thread that called
OnEventHandler.

An OnEventHandler method *can* use a thread pool thread to execute the
"MyEventHandler(this, e);" call, in which case the handlers would all
be run on the thread pool thread, but this is not as common as
invoking the delegate directly.

Signature

.NET 2.0 for Delphi Programmers        www.midnightbeach.com/.net
Delphi skills make .NET easy to learn  In print, in stores.

Stoitcho Goutsev (100) - 30 Aug 2006 21:37 GMT
Mehdi,

It depends how the class invokes the handler. If the class uses simple call
e.g. MyEvent(this, e); then all event handler will be executed by the thread
executing event invocation code. Hoever delegates supprot BeginInvoke method
that will execute the handlers in a thread from the thread pool. In addition
the class itself can create a thread to execute the event handlers.

The most common scenario is event handler to be executed synchronously  i.e.
no additional threads involved.

Signature

HTH
Stoitcho Goutsev (100)

> Hi folks,
> Consider the following code:
[quoted text clipped - 25 lines]
> Cheers,
> Mehdi

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.