I am writing a progam which downloads games from a website as zip archives and extracts them. Since all games are downloaded to the same location before being extracted I am using a Mutex to prevent race conditions when two downloads are going on at the same time. The issue is that I am getting a System.ApplicationException thrown saying: "Object synchronization method was called from an unsynchronized block of code." Everything I have looked at says that this is caused by freeing a Mutex from a thread that does not own it. However, my code Wait's and Releases the Mutex in the same thread.
await Task.Run(async () =>
{
mutex.WaitOne();
await DownloadGameAsync(ug, nvm);
await ExtractGameAsync(ug, nvm);
nvm.Action = "Done";
mutex.ReleaseMutex();
});
If I place a breakpoint on the line mutex.ReleaseMutex(); and step over the line it does not cause the exception to be thrown however, removing the breakpoint and running normally causes an exception with every execution.
Should I be approaching this differently (Semaphores) or is my use of async/await causing the issue?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…