views:

111

answers:

1

I'm working with a 3rd-party library that provides access to a database. In the database connection object, there is an event called Updated, which fires whenever the database is modified.

I'm running into a snag when my delegate wants to read from the database. It looks like the connection has a ReaderWriterLock protecting the database. During the ExecuteNonQuery call, the writer lock is taken, but not released before the event fires. So, I'm deadlocked in my delegate if I want to read.

I started writing a separate thread that would take the event from the database connection and pass it along to the registered delegates, but that felt like a sloppy solution. Am I missing an easy way to handle this problem?

What's really bugging me is that I don't always hit the deadlock case. However when I do hit it and look at the call stack, it reaches all the way back to the point where I issued the update.

By the way, the library I'm using is System.Data.SQLite.

+1  A: 

In the Updated event, call ThreadPool.QueueUserWorkItem() and pass it a method with the logic in your current Updated event callback.

Gonzalo
Nice, that is a simple approach... Is there a reason why I shouldn't just use this method for dispatching all events (not just to take care of this database issue)?
jheddings
Because you have to make sure you're code is thread-safe, and it complicates things if your application is Windows Forms or WPF based (since you cannot update the UI controls from another thread than the main thread). But when you're working on something that SHOULD NOT block when calling event handlers, such asynchronous handling is the way to go.
Yann Schwartz
That's what Control.Invoke is for. http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
Gonzalo
@jheddings: it depends on what your events do and when you want it done. Sometimes you want them to be synchronous, others you don't care ...
Gonzalo