ilerleartik
ilerleartik

Reputation: 115

Windows Phone Mutex issue

I made some researches about mutexes, but could not find anything to worth to clarify what I want as much as I can understand. I can use mutexes on linux easily. But I don't know why my mutexes won't work on my application. I also looked for some examples, and implemented it. But no way;

Here is my mutex initilaziation;

public static Mutex mutex = new Mutex(true,"mut");

Here I used my mutex to lock;

private void button4_Click(object sender, RoutedEventArgs e) //Challenge Start/Stop
        {

            StartLocationService(GeoPositionAccuracy.High);
            mutex.WaitOne();
            mutex.WaitOne();

                MessageBox.Show("I'm in mutex");
///...
        }

I did this just to see if mutex is cared by my application. But no ways, my application shows "IM in mutex" message without getting any release signal from somewhere. Normally, there must be a deadlock, but no.

What I'm trying to do is, before StartLocationService fully completed, I don't want the message to appear. I also tried mutex.ReleaseMutex(); within end of StartLocationService function. But it did not work too.

I wish semaphores had existed in WP.

Please help me; Thanks

Upvotes: 0

Views: 439

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39007

You have no deadlock because you passed 'true' for the first parameter of the Mutex, which means that the mutex is initially owned by the thread who created it. That is, your main thread.

For your example, what you must do is set the constructor's parameter to False, then call mutex.WaitOne(). This should effectively block your main thread. Then, call mutex.ReleaseMutex() at the end of the StartLocationService method.

Now that's for the theory. I wouldn't recommend you to do that, because the main thread is the UI thread. It means that the UI will become unresponsive until the location service has finished initializing, which is awful UX. You should rather display some kind of loading screen, and hide it at the end of the StartLocationService method.

Note that you can use a ManualResetEvent instead of a mutex. The end result will be the same, but it might be a tad more intuitive to use.

Upvotes: 2

Related Questions