开始新的过程,而不是产卵过程的孩子

如果不是调用进程的子进程,我将如何开始一个新进程。

例:

主程序(Caller.exe)

process.start("file.exe") 

图片:

在此处输入图像描述

如果产生进程(父)在产生的进程(子进程)之前结束,则父子链断开。 要使用它,你必须使用如下的中间存根过程:

Caller.exe→Stub.exe→File.exe。

这里Stub.exe是一个简单的启动程序,它在启动File.exe后结束。

如果你开始一个过程,那么你将成为它的父母。

也许您可以尝试从cmd.exe启动进程,因此cmd.exe将成为父进程。

 Process proc = Process.Start(new ProcessStartInfo { Arguments = "/C explorer", FileName = "cmd", WindowStyle = ProcessWindowStyle.Hidden }); 

这会运行没有父级的新进程:

 System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.FileName = @"cmd"; psi.Arguments = "/C start notepad.exe"; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; System.Diagnostics.Process.Start(psi); 

Process.Start(string fileName)的文档说

 a new process that's started alongside already running instances of the same process will be independent 

它说

 Starting a process by specifying its file name is similar to typing the information in the Run dialog box of the Windows Start menu 

这对我来说似乎与独立的过程一致。

所以根据文档, Process.Start应该按照你的意愿Process.Start

这是我现在使用的代码。 我认为这可能对某人有用。 它接受一个论点。 参数是base64编码的字符串,它解码为您要运行的文件的路径。

  Module Module1 Sub Main() Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs If CommandLineArgs.Count = 1 Then Try Dim path As String = FromBase64(CommandLineArgs(0)) Diagnostics.Process.Start(path) Catch End Try End End If End Sub Function FromBase64(ByVal base64 As String) As String Dim b As Byte() = Convert.FromBase64String(base64) Return System.Text.Encoding.UTF8.GetString(b) End Function End Module