0

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!

marczulajtis
  • 127
  • 2
  • 15
  • Are you sure the exception is caused by the use of the `Mutex`? What is the stack trace? What code is actually throwing the exception? Why _isn't_ the thread STA already? (And please tell us that you aren't actually using blanket `catch` on all exceptions without any sort of reporting to the user in your production code). – Peter Duniho Nov 04 '14 at 07:55

1 Answers1

0

Seems like the issue was not connected with Mutex, which just mislead me.

And about the catch block, I am handling exception there, but it was not the point here.

Thanks for pointing out trace, looks like I was bli

marczulajtis
  • 127
  • 2
  • 15