Hornet Dear Bernard

Dear, Dear Bernard

.net events behavior

leave a comment »

It was confusing how .net is firing the Events if we have an event in a class and we decided to fire that event, what will be the exact behavior will it fire on the same thread or another thread, also will it fire the the participating handlers at once or one after another. i thought we can simply try that (Synchronous , Asynchrounous, and Timer Events)

here is the code

[BEGIN]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

using System.Threading;

using System.Timers;

namespace WindowsFormsApplication2

{

class ClassWithEvent

{

public delegate void AsyncMethodCaller(object sender, EventArgs e);

public event EventHandler Event1;

public void Method1()

{

Console.WriteLine(“ClassWithEvent : » synchronous event firing”);

Event1(this, new EventArgs());

Console.WriteLine(“ClassWithEvent : « synchronous After event firing”);

// async call

Console.WriteLine(“ClassWithEvent : » firing asynchronous event”);

AsyncMethodCaller asyncCall = new AsyncMethodCaller(Event1);

IAsyncResult result = asyncCall.BeginInvoke(this, new EventArgs(), null, null);

Console.WriteLine(“ClassWithEvent : » Sleep 20 second”);

Thread.Sleep(20000);

Console.WriteLine(“ClassWithEvent : « Sleep 20 second”);

asyncCall.EndInvoke(result);

Console.WriteLine(“ClassWithEvent : « firing asynchronous event”);

}

}

static class Program

{

static void classWithEvent_Event1(object sender, EventArgs e)

{

Console.WriteLine(“Event handler 1, sleep 5 seconds”);

for (int i = 0; i < 5; i++)

{

Console.WriteLine(“Event Handler 1 Waiting {0}”, i);

System.Threading.Thread.Sleep(1000);

}

Console.WriteLine(“Event handler 1 returning from event handler”);

}

static void classWithEvent_Event2(object sender, EventArgs e)

{

Console.WriteLine(“Event handler 2, sleep 5 seconds”);

for (int i = 0; i < 5; i++)

{

Console.WriteLine(“Event Handler 2 Waiting {0}”, i);

System.Threading.Thread.Sleep(1000);

}

Console.WriteLine(“Event handler 2 returning from event handler”);

}

static void timer_Elapsed(object sender, ElapsedEventArgs e)

{

Console.WriteLine(“Timer Event handler”);

}

[MTAThread]

static void Main()

{

var timer = new System.Timers.Timer(100);

timer.Elapsed+= new System.Timers.ElapsedEventHandler(timer_Elapsed);

timer.Start();

System.Threading.Thread.Sleep(200);

timer.Stop();

var classWithEvent = new ClassWithEvent();

classWithEvent.Event1 += new EventHandler(classWithEvent_Event1);

classWithEvent.Event1 += new EventHandler(classWithEvent_Event2);

Console.WriteLine(“form : Calling the event class to do it’s event”);

classWithEvent.Method1();

Console.WriteLine(“form : After calling the event class to do it’s event”);

}

}

}

[END]

and the output is

Timer Event handler

Timer Event handler

Timer Event handler

form : Calling the event class to do it’s event

ClassWithEvent : » synchronous event firing

Event handler 1, sleep 5 seconds

Event Handler 1 Waiting 0

Event Handler 1 Waiting 1

Event Handler 1 Waiting 2

Event Handler 1 Waiting 3

Event Handler 1 Waiting 4

Event handler 1 returning from event handler

Event handler 2, sleep 5 seconds

Event Handler 2 Waiting 0

Event Handler 2 Waiting 1

Event Handler 2 Waiting 2

Event Handler 2 Waiting 3

Event Handler 2 Waiting 4

Event handler 2 returning from event handler

ClassWithEvent : « synchronous After event firing

ClassWithEvent : » firing asynchronous event

ClassWithEvent : » Sleep 20 second

Event handler 1, sleep 5 seconds

Event Handler 1 Waiting 0

Event Handler 1 Waiting 1

Event Handler 1 Waiting 2

Event Handler 1 Waiting 3

Event Handler 1 Waiting 4

Event handler 1 returning from event handler

Event handler 2, sleep 5 seconds

Event Handler 2 Waiting 0

Event Handler 2 Waiting 1

Event Handler 2 Waiting 2

Event Handler 2 Waiting 3

Event Handler 2 Waiting 4

Event handler 2 returning from event handler

ClassWithEvent : « Sleep 20 second

ClassWithEvent : « firing asynchronous event

form : After calling the event class to do it’s event

So it appears that normal event firing is synchronous unless we intentionally made them asynchronous

While if we used Timer’s Elapsed event it is asynchronous and getting called in a separate thread even if the main thread is blocked.

Advertisements

Written by bernardhornet

March 7, 2011 at 2:52 pm

Posted in Uncategorized

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: