How to restart one instance winforms application

I have an application that I restart at 2 in the morning every morning using

Application.Restart();

the problem is that the inspection after a couple of weeks shows that about 6 copies are working.

I tried to limit the number of instances using

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

But that did not work, because for some reason the recently stopped instance was still visible ... or at least this is my interpretation, and as a result the application did not restart.

Where am I wrong?

+3
source share
4 answers

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

+3
source

, ? , Application.Restart.

, . , . ( 1 , ).

, "" PID ( ) .

, , , , (- , ).


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // start a background thread that will never be exited.
            System.Threading.Thread thread = new System.Threading.Thread(delegate() { while (true) System.Threading.Thread.Sleep(1000); });
            thread.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }
}

, , - - ( bool ). , .

. true, , , , , . .

+3

... ?

, , , 2 .

, ... . . , , , .

+2

, , -, , , shutdown, , . , ; Shutdown Restart - , .

+2

Source: https://habr.com/ru/post/1711585/


All Articles