I've encountered an issue with Mutex class. I'm working in multi-instance WPF application and wanted to lock specific process from being entered by all other instance except the one that firstly called a method.
So I've created a new mutex object:
private static Mutex lockMutex = new Mutex(false, "LockOnTaskOpen");
and then in a method I did the following:
try
{
if (lockMutex.WaitOne(500))
{
try
{
// doing my code here
}
catch (Exception e)
{
ex = e;
}
finally
{
lockMutex.ReleaseMutex();
}
}
else
{
return;
}
}
catch
{
}
And the problem is that one instance is entering a try and works ok, and the other instance goes to else block (so also ok), but then it catches an exception which is this one:
"The calling thread must be STA, because many UI components require this."
If anyone is able to help me solve this issue, than I'll be thankful.
Many thanks!