1

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);

2 Answers 2

2

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.

1
  • Thanks, so I will have to prepare for some indeterministic behaviour :)
    – nan
    Sep 3, 2010 at 13:14
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.