C#:如何在程序退出后运行批处理(在此期间不调用)

时间:2022-10-19 20:48:32

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).

MainApp.exe检查更新,下载最新更新。问题是有时更新是MainApp.exe当前使用的DLL。所以,我想可能只是将所有文件转储到Update(temp)文件夹中,当程序退出时运行批处理文件,覆盖DLL然后重新启动程序。这很常见,我已经看到它已经完成(例如Spybot Search and Destroy)。

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.

哦,我怀疑这在WPF应用程序中会有什么不同,但是如果它是......我正在将我的应用程序编写为WPF应用程序。

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

附:我认为在Unix中类似的是Forking Exec?

3 个解决方案

#1


8  

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

在第一个应用中,将第二个应用传递给您的进程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 */
}

#2


2  

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

您可以尝试从wpf applciation的Exit事件启动第二个进程。 [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();
            };
    }
}

#3


0  

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)

1)创建您的WPF应用程序,并根据WPF应用程序中发生的事情,在您的WPF应用程序中设置退出代码(例如,Application.Current.Shutdown(123)将cmd行上的%errorlevel%变量设置为123)

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

2)创建一个主批处理(main.cmd),它将执行所有控制:


Content of main.cmd:

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.

3)您只启动主Batch.cmd,它执行所有控制。

The benefits of this Approach are:

这种方法的好处是:

  1. You only Need 1 Batch and one WPF app
  2. 您只需要1个批处理和一个WPF应用程序

  3. You can very easily control within one Batch what should happen when specific Actions are selected within the WPF app
  4. 您可以非常轻松地在一个批处理中控制在WPF应用程序中选择特定操作时应该发生的情况

  5. You can do as many update Loops as needed and then exit
  6. 您可以根据需要执行尽可能多的更新循环,然后退出

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.

提示:如果您不喜欢黑色CMD窗口保持打开状态,则必须使用从WPF应用程序中取出的一些操作来隐藏它。

With kind regards,

亲切的问候,

Michael

#1


8  

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

在第一个应用中,将第二个应用传递给您的进程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 */
}

#2


2  

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

您可以尝试从wpf applciation的Exit事件启动第二个进程。 [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();
            };
    }
}

#3


0  

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)

1)创建您的WPF应用程序,并根据WPF应用程序中发生的事情,在您的WPF应用程序中设置退出代码(例如,Application.Current.Shutdown(123)将cmd行上的%errorlevel%变量设置为123)

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

2)创建一个主批处理(main.cmd),它将执行所有控制:


Content of main.cmd:

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.

3)您只启动主Batch.cmd,它执行所有控制。

The benefits of this Approach are:

这种方法的好处是:

  1. You only Need 1 Batch and one WPF app
  2. 您只需要1个批处理和一个WPF应用程序

  3. You can very easily control within one Batch what should happen when specific Actions are selected within the WPF app
  4. 您可以非常轻松地在一个批处理中控制在WPF应用程序中选择特定操作时应该发生的情况

  5. You can do as many update Loops as needed and then exit
  6. 您可以根据需要执行尽可能多的更新循环,然后退出

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.

提示:如果您不喜欢黑色CMD窗口保持打开状态,则必须使用从WPF应用程序中取出的一些操作来隐藏它。

With kind regards,

亲切的问候,

Michael