Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • Thanks for your quick reply and fixing my code. Omitting mutex.WaitOne() only happened when shortening my production code for the test sample. With this fix my test no longer produces the exception mentioned. Commented Mar 7, 2016 at 14:09
  • Now I can no longer reproduce the exception. Simply using the mutex without any additional measures seems to work fine. I saw you posted a similar question Named Mutex with await and settled for a solution based on a custom TaskScheduler. I am no longer shure a problem synchronizing async file IO with a named mutex exists. What do you think? Commented Mar 7, 2016 at 14:31
  • @PeterMeinl In UWP direct thread managing is not possible, therefore I'm not sure now, how the solution with custom TaskScheduler would look like. Though, as I think, the LongRunning task should work - it dedicates single thread for a task and after returning from await it should be possible to safely release mutex. If your async operation is not long you may also consider synchronous waiting instead of awaiting after acquiring mutex. The best would be just to test it - maybe create a sample BTask, open a file and to the same in main UI (both with global mutex). Commented Mar 7, 2016 at 16:51
  • My current App does synchronizes the UI with a BackTask using a named mutex and seems to work fine. As a precaution it traces mutex .WaitOne(_preventHangingTimespan) timeouts and .ReleaseMutex exceptions and reports them via ApplicationInsights. But Stephen Cleary writes in WP8 Mutex - Resource Locking "You can't use thread-affine locks with async code" and this makes me wonder if I missed something. Commented Mar 7, 2016 at 18:03
  • @PeterMeinl The best would be to ask Stephen Cleary :) - maybe you can ensure if you can combine LongRunning task with mutex and awiat. IMO you can mix await and mutex, but you have to ensure that the same thread acquires and releases the mutex. Commented Mar 7, 2016 at 18:55