How can I write to a file using await FileIO.WriteTextAsync()
(in Windows Phone 8.1) after acquiring mutex
so that no two threads access the same file and mutual exclusion is ensured. I'm doing the following:
mutex.WaitOne()
try
{
await FileIO.WriteTextAsync(filename, text);
Debug.WriteLine("written");
}
finally
{
mutex.ReleaseMutex();
}
But the method works for one or two iterations only, after which it throws a System.Exception
. Also, if I remove the await
keyword or remove the file writing method entirely, the code runs perfectly fine. So, all trouble is caused by calling an async method. What can I do to resolve this?