Enviroment.Exit 后开始进程
Start Process after Enviroment.Exit
我的问题:我想加一个重启功能,这个没有问题,但是我需要在关闭程序后启动它,因为程序只能有一个实例。我如何在制作 Enviroment.Exit 之后以某种方式启动一个进程(本身)?
根据您的需要,如果这是一个 Windows 表单应用程序。这可能对您有帮助:
它一直存在,他们将它放回到 .net Core 3.0
- .NET 5.0
- .NET 核心 3.0、3.1
- .NET 框架 2.0、3.0、3.5、4.0、4.5、4.5.1、4.5.2、4.6、4.6.1、4.6.2、4.7、4.7.1、4.7.2、4.8
Shuts down the application and starts a new instance immediately.
Remarks
The most common reason for calling Restart is to start a new version
of the application that you have downloaded through ClickOnce using
the Update or UpdateAsync method.
Applications are restarted in the context in which they were initially
run. If your application was started using a URL pointing directly to
the application's main executable file, it will be restarted using the
same URL. If your application is a ClickOnce application, it will be
restarted using ClickOnce.
If your application was originally supplied command-line options when
it first executed, Restart will launch the application again with the
same options.
你不应该做Environment.Exit
,想想其他运行话题?
您可以使用以下代码重新启动。
static void RestartProcess(int processId, string processName )
{
Process process = null;
try
{
process = Process.GetProcessById(processId);
Application.Exit();
//Or you can use process.WaitForExit(1000);
}
catch (ArgumentException ex)
{
}
Process.Start(processName);
}
解决方案是,感谢 ckuri (https://whosebug.com/users/7565574/ckuri) :
“你显然有一些代码来检查一个实例是否已经 运行,如果一个是 运行,则当前应用程序的启动将被中止。重启问题的一种解决方案是使用说明跳过实例检查的参数启动新进程。"
我的问题:我想加一个重启功能,这个没有问题,但是我需要在关闭程序后启动它,因为程序只能有一个实例。我如何在制作 Enviroment.Exit 之后以某种方式启动一个进程(本身)?
根据您的需要,如果这是一个 Windows 表单应用程序。这可能对您有帮助:
它一直存在,他们将它放回到 .net Core 3.0
- .NET 5.0
- .NET 核心 3.0、3.1
- .NET 框架 2.0、3.0、3.5、4.0、4.5、4.5.1、4.5.2、4.6、4.6.1、4.6.2、4.7、4.7.1、4.7.2、4.8
Shuts down the application and starts a new instance immediately.
Remarks
The most common reason for calling Restart is to start a new version of the application that you have downloaded through ClickOnce using the Update or UpdateAsync method.
Applications are restarted in the context in which they were initially run. If your application was started using a URL pointing directly to the application's main executable file, it will be restarted using the same URL. If your application is a ClickOnce application, it will be restarted using ClickOnce.
If your application was originally supplied command-line options when it first executed, Restart will launch the application again with the same options.
你不应该做Environment.Exit
,想想其他运行话题?
您可以使用以下代码重新启动。
static void RestartProcess(int processId, string processName )
{
Process process = null;
try
{
process = Process.GetProcessById(processId);
Application.Exit();
//Or you can use process.WaitForExit(1000);
}
catch (ArgumentException ex)
{
}
Process.Start(processName);
}
解决方案是,感谢 ckuri (https://whosebug.com/users/7565574/ckuri) :
“你显然有一些代码来检查一个实例是否已经 运行,如果一个是 运行,则当前应用程序的启动将被中止。重启问题的一种解决方案是使用说明跳过实例检查的参数启动新进程。"