【发布时间】:2012-01-16 02:09:01
【问题描述】:
【问题讨论】:
-
只是好奇,它是子进程而不是父进程的含义是什么?
-
如果用户在任务管理器中选择“杀死进程树”,我的整个应用程序不会死。
-
闻起来还是像XY problem。新进程不是当前进程的子进程或规避“杀死进程树”的真正意义是什么?
标签: c# vb.net process parent-child
【问题讨论】:
标签: c# vb.net process parent-child
如果生成进程(父进程)在生成进程(子进程)之前结束,则父子链中断。要利用这一点,您必须像这样使用中间存根进程:
Caller.exe → Stub.exe → File.exe。
这里的 Stub.exe 是一个简单的启动程序,在启动 File.exe 后就结束了。
【讨论】:
Webbrowser 控件渲染了一个嵌入式 IE 组件,以及在该组件中运行的目标第三方黑盒类型网页一直挂起,直到父浏览器重新获得焦点或被终止。从 Java 中删除 .exe 应用程序完全解决了这个问题。
Process.GetProcesses()检索分离的程序?
如果你启动一个进程,那么你就是它的父进程。
也许您可以尝试从 cmd.exe 启动您的进程,因此 cmd.exe 将是父进程。
Process proc = Process.Start(new ProcessStartInfo { Arguments = "/C explorer", FileName = "cmd", WindowStyle = ProcessWindowStyle.Hidden });
【讨论】:
start 从 cmd 启动进程?
这会在没有父进程的情况下运行新进程:
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);
【讨论】:
我一直在尝试启动一个更新进程,该进程会删除调用进程的文件并用新文件替换它们。通过设置UseShellExecute = true,我能够在调用进程退出时规避生成的进程退出。
这是在使用 WPF 的 .Net Core 3.0 应用程序中。
var startInfo = new ProcessStartInfo("Updater.exe");
startInfo.UseShellExecute = true;
Process.Start(startInfo);
Environment.Exit(0);
【讨论】:
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 独立产生的。然后我尝试在一个简单的测试项目中隔离行为,我得出结论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
【讨论】: