Resolved How to close and restart an application?

NPC1977

Member
Joined
Sep 30, 2021
Messages
5
Programming Experience
Beginner
Hi

I'm trying to put together a console app that kills off an existing process and then reloads it, and then closes the console window (would prefer the console window to be invisable if thats poss?).

My code is:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Demo_Console
{
    class Program
    {
        public void Kill(program.exe);

        static void Main(string[] args)
        {
            Process ExternalProcess = new Process();
            ExternalProcess.StartInfo.FileName = "C:\\folder\\program.exe";
            ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            ExternalProcess.Start();
            ExternalProcess.WaitForExit();

            public bool CloseMainWindow ();
        }
    }
}

It seems to launch my app, but doesnt seem to close the instance in the first place. Can anyone point out where I am going wrong please? Also, any pointers in adding a small time delay in closing and relaunching the .exe?

Thanks in advance
 
Last edited by a moderator:
Traditionally, if you are trying to kill your own instance, and then restart, most people use the task scheduler APIs and put in a task to start the program a small amount of time into the future, and then they just let the current instance finish running.

For people who don't want to fiddle with the task scheduler APIs, they usually deploy a stub program whose only job is wait for the current instance of the master program to die (using a mutex associated with the master process), and then launch another instance of the master program (and then the stub can terminate normally).

In general, you can mark a console program as a Windows program so that a console window is not automatically created and associated with it.

Now as for your problem, it looks like you are trying to write a variation of that stub program that I just described above. I don't see where you are trying to kill or wait for the original instance to die. I only see you launching a new instance and then waiting for it to die. Perhaps you should use enumerate the current running processes to see if the master program is running. And it is, send it a signal to terminate, and the wait for it to exit. Once it is dead then you can launch the master program again. Then you can just let your program end. There is no need to wait for that newly started process to exit.
 
Back
Top Bottom