The official document: Application. Restart Method (System. Windows. Forms) | Microsoft Docs
directory
Restart
explore
conclusion
revelation
Restart
When we use it in the.net Framework, there is nothing special about it and it works, but when we use it in.net core3.0, we throw an InvalidOperation exception.
Application.Restart();
Copy the code
Why is there such a difference in the same method?
explore
Let’s first look at the exception information:
Process was not started by this object, so requested information cannot be determined.
This object did not start the process, so the requested information cannot be determined.
From this we can infer that this exception is related to process start. The code for the Restart method consists of two parts:
if (string.Equals(str + "\ieexec.exe", currentProcess.MainModule.FileName, StringComparison.OrdinalIgnoreCase))
{
flag = true;
ExitInternal();
string text = AppDomain.CurrentDomain.GetData("APP_LAUNCH_URL") as string;
if (text != null)
{
Process.Start(currentProcess.MainModule.FileName, text);
}
}
Copy the code
ProcessStartInfo startInfo = Process.GetCurrentProcess().StartInfo;
startInfo.FileName = ExecutablePath;
if (stringBuilder.Length > 0)
{
startInfo.Arguments = stringBuilder.ToString();
}
ExitInternal();
Process.Start(startInfo);
Copy the code
The first piece of code is mainly for executing IEExec.exe, so we won’t focus on it.
The ieexec.exe application is. NET Framework with a program that exists in multiple system whitelists. The IEexec.exe application can be used as a host to run other managed applications started with a URL.
Let’s look at the second part of the code about processes:
ProcessStartInfo startInfo = Process.GetCurrentProcess().StartInfo;
Process.Start(startInfo);
Copy the code
When I perform, indeed as expected in Process. The GetCurrentProcess () StartInfo InvalidOperation abnormalities, abnormal at this point we will narrow the StartInfo properties.
The.net core version 3.0
public static Process GetCurrentProcess()
{
throw null;
}
Copy the code
public ProcessStartInfo StartInfo
{
get
{
throw null;
}
set
{
}
}
Copy the code
The.net Framework version
public static Process GetCurrentProcess()
{
return new Process(".", isRemoteMachine: false, Microsoft.Win32.NativeMethods.GetCurrentProcessId(), null);
}
Copy the code
public ProcessStartInfo StartInfo { get { if (startInfo == null) { startInfo = new ProcessStartInfo(this); } return startInfo; } set { if (value == null) { throw new ArgumentNullException("value"); } startInfo = value; }}Copy the code
conclusion
At this point we conclude that in.net core3.0, a subsequent call to the StartInfo property will throw an exception anyway because GetCurrentProcess() returns null and the StartInfo get is not instantiated.
revelation
.NET Core 3.0 is the first major release of the.NET Core framework to support desktop applications, and some problems are normal. But as a programmer, you should focus on stability, not just the thrill of new technology. Finally, HERE’s a quote from Lao Tzu for you as you read:
I have three treasures, which I hold and keep. One day kindness, two days thrift, three days do not dare to be the first. Ci Gueng yong; Frugality and energy fixation; Don’t dare to be the first.