views:

50

answers:

2

Does SqlDependency (for example) fires the OnChange event, when the invocation of previous event handler didn't finish? (Assume that OnDependencyChange method is very time consuming) What exactly happens?

SqlDependency dependency=new SqlDependency(command);


// Subscribe to the SqlDependency event.
dependency.OnChange += new
OnChangeEventHandler(OnDependencyChange);
A: 

In normal single threaded program not it will not. Try following:

Add windows forms app. Add a button to a form. Use following method to handle button's click event:

private void Buttonclick(object sender, EventArgs e)
{
    Thread.Sleep(25);//SUSPEND CURRENT THREAD
}

you will not be able to click the button until the call to Thread.Sleep(25) finishes its work (suspend thread for 25 secs). You can test this in Console app also.

TheVillageIdiot
+1  A: 

I am not that familiar with the SqlDependency class, but per the MSDN documenation:

The OnChange event may be generated on a different thread from the thread that initiated command execution.

That seems to open up the possibility that two event handlers can run simultaneously. There is no documentation that states that all event handlers invoked from raising the event must complete before the event can be raised again. The safe thing to do is to assume multiple simultaneous invocations of the event can occur. Because the event is raised on an undefined thread you will have to guard against the problems of concurrency anyway.

Brian Gideon
Thanks, so I will have to prepare for some indeterministic behaviour :)
Andrzej Nosal