djmj
djmj

Reputation: 5544

SQLDependency - does not fire while being busy

Is it normal behavior that a SQLDependency onchange event is not fired if its thread is too busy?

    private void NotificationOnChanged(...)
    {
        // get database results            

        // simulate long process 
        Thread.Sleep(10000);
    }

During the sleep i add a new row and the notification is lost after the sleep expires.

Must I spawn a new single thread to do the work and use a flag to detect if new notifications arrived to restart the it?

Upvotes: 2

Views: 140

Answers (1)

Remus Rusanu
Remus Rusanu

Reputation: 294307

This behavior is an artifact of the implementation on the ADO.net notification listener. See http://rusanu.com/2008/01/04/sqldependencyonchange-callback-timing/.

The SqlDependency internal thread that posts the WAITFOR(RECEIVE) is not going to post another one until the callback returns. So you have to do as little processing as possible and return in the OnChange event. Definetely nothing blocking.

Alternatively you can use the lower level SqlNotificationRequest that lets you manage everyting, including the notification handling. But you'll have to manage everything. See http://technet.microsoft.com/en-us/library/ms190270(v=sql.105).aspx

Upvotes: 3

Related Questions