简体   繁体   English

Application.Restart如何在.NET中运行?

[英]How does Application.Restart work in .NET?

This question is not about how to restart an application. 此问题与如何重新启动应用程序无关。 I am already achieving that by using a Mutex and a secondary starter application. 我已经通过使用Mutex和辅助启动器应用程序实现了这一目标。 I had to resort to that after facing some problems using Application.Restart. 在使用Application.Restart遇到一些问题后,我不得不求助于此。

In any case, not being fluent with IL, I was wondering if someone could explain how Application.Restart works in the first place. 无论如何,我不熟悉IL,我想知道是否有人可以解释Application.Restart的工作原理。 It is a call to the runtime but what exactly does the runtime do? 它是对运行时的调用,但运行时究竟做了什么? How does it close the existing instance and how does it know when to launch a new one? 它如何关闭现有实例以及它如何知道何时启动新实例?

... not being fluent with IL, ... ......不熟悉IL,......

Have you considered using a decompiler (Reflector, dotPeek) or, even better, the reference source code of the .NET framework? 您是否考虑过使用反编译器 (Reflector,dotPeek),或者更好的是使用.NET框架的参考源代码?

Anyway. 无论如何。

At a casual look it does the following: 随便看看它做了以下事情:

  • In all below cases, the current instance is terminated using Application.ExitInternal() . 在以下所有情况下,使用Application.ExitInternal()终止当前实例。 That is the gist of the public Application.Exit() method, eliding some security checks/asserts. 这是公共Application.Exit()方法的要点,忽略了一些安全检查/断言。

  • See if it can determine the Assembly.GetEntryAssembly() . 看看它是否可以确定Assembly.GetEntryAssembly() If that is null the call the Application.Restart() was most likely done from unmanaged code and the operation throws a NotSupportedException to the caller. 如果为null则调用Application.Restart()很可能是从非托管代码完成的,并且操作会向调用者抛出NotSupportedException

  • See if the current process is ieexec.exe , if so use it to restart the application (for more information about ieexec.exe see here ). 查看当前进程是否为ieexec.exe ,如果是,请使用它来重新启动应用程序(有关ieexec.exe详细信息,请参阅此处 )。 Actually that is pretty much also a Process.Start() call to ieexec.exe , but the command line arguments are not gathered by Environment.GetCommandLineArgs() (see below), but by reading the APP_LAUNCH_URL application domain data. 实际上,这也是对ieexec.exeProcess.Start()调用,但命令行参数不是由Environment.GetCommandLineArgs() (见下文)收集的,而是通过读取APP_LAUNCH_URL应用程序域数据。

  • See if the application is a click-once application ( ApplicationDeployment.IsNetworkDeployed ), if so call an CLR internal native code to (re)launch that: CorLauncApplication . 查看应用程序是否为单击一次应用程序( ApplicationDeployment.IsNetworkDeployed ),如果是,则调用CLR内部本机代码以(重新)启动: CorLauncApplication The only publicly available source code that somewhat resembles the native parts of the CLR is the shared source CLI (sscli), which is based on the .NET 2.0 framework and also is partly incomplete. 唯一公开可用的源代码有点类似于CLR的本机部分,是共享源CLI(sscli),它基于.NET 2.0框架,也部分不完整。 It contains a definition for that function ( clr\\src\\vm\\hosting.cpp ), but it is only a stub. 它包含该函数的定义( clr\\src\\vm\\hosting.cpp ),但它只是一个存根。 In the end it will use some means to restart the process (eg Win32's CreateProcess API). 最后,它将使用一些方法来重新启动进程(例如Win32的CreateProcess API)。

  • Else: the application is a "regular" .NET application. 否则:该应用程序是一个“常规”.NET应用程序。 Environment.GetCommandLineArgs() is used to recreate the original command line and Process.Start(Application.ExecutablePath) is used to restart the application. Environment.GetCommandLineArgs()用于重新创建原始命令行, Process.Start(Application.ExecutablePath)用于重新启动应用程序。

The use of the Application.Exit -mechanism to try to end the current instance is probably the reason why you find it unreliable. 使用Application.Exit -mechanism来尝试结束当前实例可能是您发现它不可靠的原因。 Forms that cancel the send closing event can interrupt it. 取消发送结束事件的表单可以中断它。 Also see this SO question. 另见这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM