简体   繁体   中英

how do I restart a single instance winforms application

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?

确保在应用程序退出事件上挂起释放互斥锁并关闭互斥锁的方法。

By any chance, are you using multiple threads? If you don't shut down your background threads, they will keep your process running, even through a call to Application.Restart.

I have pasted in some code below that demonstrates this behavior. To see it, compile a test project with the code below and run it. (you will need to put 1 button on the form and assign the click handler that I have defined in the code below).

Start Task Manager, go to the Process tab, and make sure to add the PID (process id) column to the view.

Each time you click the button, the app will restart, but you should see the old process still hung in memory (due to a background thread that was not closed down).


    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();
        }
}

Assuming that this is your problem, the best way to correct it is to put some kind of a check into your background threads (even a bool flag will do). Have them check for exited periodically and and exit gracefully when your app shuts down.

Note: you could set the thread's background property to true, and it will be exited automatically, but if you do that, you don't have control over which instruction the thread is executing when it exits, so you can't perform any type of cleanup. It's best to code your own check.

I have run into this problem in the past, and what I believe the issue was is that the mutex of the running instance getting shutdown was not being released before the mutex of the second instance, starting up was checking for it. To solve this, what I did was provided a means to pass back control to my main form with an indication to restart; so that the Shutdown part of the Restart didn't have to perform any tasks except for exiting.

First off... why do you need to restart an app every day?

I'm guessing theres a better solution than an app that restarts itself at 2 AM every day.

For example, you may have a memory leak... as described in your comment. Addressing that might be a better place to focus your efforts. Restarting an app preiodically to avoid a memory leak would be considered by most programmers, including myself, a hack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM