I have one little task - i have to create mutex,using tasks and async\await,that not blocking current thread(UI thread).
Mutex interface:
Lock() – returns a Task, wich will be completed only when mutex will become free. This task must be unique per lock to disallow more than 1 unblock per Release() call. Release() – release mutex. One and only one of tasks, returned by Lock(), must become completed. That mutex have to work in a such way
await mutex.Lock();
// critical code
mutex.Release();
I wrote some piece of code(see link in the end),but if briefly describe the problem is: I create a class Mutex. In the method Lock i do something like that:
await Task.Factory.StartNew(() =>
{
mutex.WaitOne();
UserDispatcher = Dispatcher.CurrentDispatcher;
});
In the method Release i do something like that:
if (mutex != null && UserDispatcher != null)
{
UserDispatcher.Invoke(new Action(() =>
{
Console.WriteLine("Releasing");
mutex.ReleaseMutex();
}));
}
When i`m trying release mutex,my program just freeze forever. What the problem?
Full code: https://www.dropbox.com/s/4a370cn1bd2o0nz/MainWindow.xaml.cs?dl=0
Link to project: https://www.dropbox.com/sh/qsgle79jdgpr7cd/AAA3MZ6VlTNbUj-isgaQaLE-a?dl=0