开发者

C#: How do I run a batch process after a program exits (not called during it)

I have the following setup:

MainApp.exe checks for updates, downloads latest updates. The problem is that sometimes the updates are DLLs that the MainApp.exe is currently using. So, I thought maybe just dump all the files into an Update (temp) folder and when the program exits run a batch file that overwrites the DLLs and then relaunches the program. This is common, I've seen it be done (Spybot Search and Destroy for example).

My question is how do you make a program run a process AFTER it exits?

Alternatively, can a batch program be called DURING the program, but wait until AFTER the program is closed to start it's actual batch?

Oh,开发者_如何学运维 and I doubt this would be any different in a WPF Application, but in case it is... I'm writing my App as a WPF App.

P.S. I think in Unix something similar is a Forking Exec?


In the first app, pass the second app your process id:

using System.Diagnostics;

static void Main(){
    /* perform main processing */
    Process.Start("secondapp.exe", Process.GetCurrentProcess().Id.ToString());
}

In the child process, wait for the first to exit:

using System.Diagnostics;

static void Main(string[] args){
    Process.GetProcessById(int.Parse(args[0])).WaitForExit();
    /* perform main processing */
}


You could try, initiating the second process from Exit event of the wpf applciation. [App.Xaml.cs]

public partial class App : Application
{
    public App()
    {
        this.Exit += (s, e) =>
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = new System.Diagnostics.ProcessStartInfo("notepad.exe");
                p.Start();
            };
    }
}


Why not:

1) create your WPF app and according to what happend within the WPF app, set the Exit code within your WPF app (e.g. Application.Current.Shutdown(123) sets the %errorlevel% variable on the cmd line to 123)

2) create a main Batch (main.cmd) which will do all the control:


Content of main.cmd:

@echo off

rem *** Start the WPF app here  and wait until it Closes ****
:Start_WPF
start /wait wpf-app.exe

rem *** Assuming that exitcode 123 from your WPF app means: don't do any update, jump to end of main.cmd and do nothing ***
if "%errorlevel%"=="123" goto endofbatch

rem *** Update your app by overwriting formerly blocked DLLs ***

rem // insert update copy steps here //

rem *** Restart your WPF app by Looping back to Start_WPF ***
goto Start_WPF

rem *** End of main.cmd ***
:endofbatch

3) you only start the main Batch.cmd which does all the control.

The benefits of this Approach are:

  1. You only Need 1 Batch and one WPF app
  2. You can very easily control within one Batch what should happen when specific Actions are selected within the WPF app
  3. You can do as many update Loops as needed and then exit

hint: If you don't like the black CMD window staying open, you will have to hide it with some Actions taken out of the WPF app.

With kind regards,

Michael

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜