2

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?

4

1 回答 1

2

有用于跨进程同步的对象,使用它们比使用文件发出信号要好得多。您可能会考虑使用带有名称的EventWaithHandleMutex进行全局同步。它可以在多个进程(不仅是线程)之间工作。您可以在 Alabhari 的博客中找到这种同步的一个很好的例子。

在您的情况下,如果设置了句柄,则意味着其他进程如果正在执行工作并且当前进程可以返回。

至于等待句柄,您可以在这个问题上找到更多信息。

于 2015-12-04T12:19:40.123 回答