Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:31:58+00:00 2026-06-06T23:31:58+00:00

I’m writing a windows phone app which stores data in a local database. There

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:32:00+00:00Added an answer on June 6, 2026 at 11:32 pm

    Ok so first of all my problem was actually 2 problems:

    1. Need to ensure that if the foreground app is running, the background process won’t run
    2. Need to ensure that only one thread can access the databasse at once and this needs to work across processes to cater for the (admittedly rare, but possible) scenario where the foreground app is started while the background process is in progress.

    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:

    /// <summary>
    /// Used to ensure only one instance (foreground app or background app) runs at once
    /// </summary>
    public class SingleInstanceSynchroniser : IDisposable
    {
        private bool hasHandle = false;
        Mutex mutex;
    
        private void InitMutex()
        {
            string mutexId = "Global\\SingleInstanceSynchroniser"; 
            mutex = new Mutex(false, mutexId);
        }
    
        public SingleInstanceSynchroniser()
        {
            InitMutex();
            hasHandle = mutex.WaitOne(0);
        }
    
        public void Dispose()
        {
            if (hasHandle && mutex != null)
                mutex.ReleaseMutex();
        }
    
        public bool HasExclusiveHandle { get { return hasHandle; } }
    
    }
    

    Usage:

    In App.xaml.cs:

    ...
    
    SingleInstanceSynchroniser singleInstanceSynchroniser;
    
    public App()
    {
        singleInstanceSynchroniser = new SingleInstanceSynchroniser();
    
    ...
    

    In ScheduledAgent.cs:

    SingleInstanceSynchroniser singleInstanceSynchroniser;
    
    protected override void OnInvoke(ScheduledTask task)
        {
            singleInstanceSynchroniser = new SingleInstanceSynchroniser();
    
            if (singleInstanceSynchroniser.HasExclusiveHandle)
            {
                //Run background process
                ...
            }
            else
            { //Do not run if foreground app is running
                NotifyComplete();
            }
        }
    

    To solve problem (2), I created the SingleAccessSynchroniser:

    /// <summary>
    /// Used to ensure only one call is made to the database at once 
    /// </summary>
    public class SingleAccessSynchroniser : IDisposable
    {
        public bool hasHandle = false;
        Mutex mutex;
    
        private void InitMutex()
        {
            string mutexId = "Global\\SingleAccessSynchroniser"; 
            mutex = new Mutex(false, mutexId);            
        }
    
        public SingleAccessSynchroniser() : this(0)
        { }
    
        public SingleAccessSynchroniser(int TimeOut)
        {
            InitMutex();
    
            if (TimeOut <= 0)
                hasHandle = mutex.WaitOne(); 
            else
                hasHandle = mutex.WaitOne(TimeOut);
    
            if (hasHandle == false)
                throw new TimeoutException("Timeout waiting for exclusive access on SingleInstance");
        }
    
        public void Release()
        {
            if (hasHandle && mutex != null)
            {
                mutex.ReleaseMutex();
                hasHandle = false;
            }
        }
    
        public void Dispose()
        {
            Release();
        }
    }
    

    Usage: In all database calls:

    using (var dbSync = new SingleAccessSynchroniser())
            {
                //Execute your database calls
            }
    

    This has been running reliably for a few weeks now. Hope someone else finds it useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.