Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I restart a single instance winforms application

Tags:

c#

I have an application that I restart at 2 am every morning using

Application.Restart();

the problem is that a inspection a couple of weeks later shows that there are 6 or so instances running.

I tried to limit the number of instances by using

 bool IsOwned;
 Mutex m = new Mutex(true, Name, out IsOwned);
 if (!IsOwned)
         Environment.Exit(0);

But this didn't work as for some reason the recently stopped instance was still visible...or at least that's my interpretation and as a result the application didn't reatart.

Where have I gone wrong?

like image 236
Brad Avatar asked Nov 18 '25 00:11

Brad


1 Answers

Make sure that you hook on application exit event a method that releases the mutex and closes it.

like image 137
Konstantinos Avatar answered Nov 19 '25 14:11

Konstantinos