Search code examples
windows-runtimewindows-phone-8.1windows-store-appssynchronizationwindows-8.1-universal

Controlling background task instances


Is there any way to synchronize several instances of one background task?

I'm working on a universal windows 8.1 store project. I have a background task that receives raw notifications and downloads and updates some data (hopefully meeting the CPU quota and running time limitations).

I want to avoid running background work simultaneously on Windows Phone when several pushes arrive at once. That is, background task should check if another instance of it is running and exit quietly.

The only way I could think of was through a semaphore file in local storage... But since all file IO on WP is asynchronous, I don't see how I could get it working without race conditions.

Any ideas?


Solution

  • There are objects for cross-process synchronization and using them is much better that signaling with a file. You may think of using EventWaithHandle or Mutex with name for global synchronization. It can work among multiple processes (not only threads). A good example of this synchronization you can find at Alabhari's blog.

    In your case if the handle is set then it means that other process if performing work and current one can return.

    As for waithandles you may find more information at this question.