I’m writing a windows phone app which stores data in a local database. There are multiple threads in my app that access the database and up until this point I have used the technique described here with an AutoResetEvent to ensure that only one thread can access the database at any one time.
So far this has worked very reliably, but now I want to add a ScheduledTask to do some work in the background so I’ve potentially got multiple processes now competing for access to the database.
Can anyone advise how I can adapt the AutoResetEvent technique to be used across multiple processes on Windows Phone?
I have seen approaches using a Mutex. If I acquire the Mutex before each DB call and then release it afterwards (similar to the way I’m using AutoResetEvent), will this do the trick? Is there any potential problems with this technique? eg: performance?
Ok so first of all my problem was actually 2 problems:
Based on the good work done in this thread, I created a couple of classes to help.
To solve problem (1), I created the SingleInstanceSynchroniser:
Usage:
In App.xaml.cs:
In ScheduledAgent.cs:
To solve problem (2), I created the SingleAccessSynchroniser:
Usage: In all database calls:
This has been running reliably for a few weeks now. Hope someone else finds it useful.