简体   繁体   中英

Console application fire a process then close itself

I'm writing a small updater for my app. My flow would be like this: app.exe -> call process(updater.exe) -> app.close() Then, updater check if app is closed, then overwrites app.exe and other satellite assemblies.

So I need to do something like this: launch my C# exe app, fire a process for updater.exe, then close app, but without closing child process.

There's a way to build this kind of fire-and-forget process in .NET?

Thank you, Nando

Look at the Process object. You would just call Process.Start like so:

System.Diagnostics.Process.Start("updater.exe");

Yes, I'm doing so, but seems that Process don't start...

I made a small helper class, called Updater:

Updater.CheckUpdates() 

--> starts a Process who call "updater.exe -check", and that works (when process finished, control return to my main app). I evaluate return code of process, and the I set Updater.UpdatesAvalilable bool flag, if necessary.

Updater.ApplyUpdates()

--> starts a Process who call "updater.exe -update", that do the update work.

So, my code is like this:

    Updater.CheckUpdates();
    if (Updater.UpdatesAvailable)
    {
        Updater.ApplyUpdates();
        System.Environment.Exit(0);
    }

Process in Updater.ApplyUpdates() never run. I know, is not elegant code, but I don't know how to achieve my goal. :-)

Thank you! Nando

Good morning guys. I found a way to make it work, it seems. In my helper class I wired events for getting stdIO and stdError, just to log something; removing those, I get my work done: process start and main exit! :-)

Just to make all know about it: my process declaration is now like this:

            Process process = new Process();
        process.EnableRaisingEvents = true;

        process.StartInfo = new ProcessStartInfo();
        process.StartInfo.Arguments = "-update";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.ErrorDialog = false;
        process.StartInfo.FileName = "updater.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();

        process.Start();

Thank you all! Nando

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