在什么情况下不的的Process.Start()方法返回false? [英] In what cases does the Process.Start() method return false?

查看:122
本文介绍了在什么情况下不的的Process.Start()方法返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN

返回值true,则表示新进程的资源是    启动。 如果进程资源指定的文件名成员    该StartInfo的属性是计算机,在已经运行无    额外的进程资源开始。取而代之的是,在运行过程中    资源再利用并返回false。

The return value true indicates that a new process resource was started. If the process resource specified by the FileName member of the StartInfo property is already running on the computer, no additional process resource is started. Instead, the running process resource is reused and false is returned.

尝试是这样的:

var info = new ProcessStartInfo {FileName = @"CMD"};

var p1 = new Process
{
     StartInfo = info
};

var result = p1.Start(); //true
result = p1.Start(); //true

var p2 = new Process
{
    StartInfo = info
};

result = p2.Start(); //true

如果我使用有同样的结果文件路径= @C:\ MyApp.exe的而不是 CMD

Have the same result if I'm using FilePath = @"c:\myapp.exe" instead of CMD.

在什么情况下,它返回

In what cases does it return false?

推荐答案

如果你看一看的参考源,您将看到如何的Process.Start 的工作原理:

If you take a look at the reference source, you'll see how Process.Start works:

<一个href="http://referencesource.microsoft.com/System/R/c50d8ac0eb7bc0d6.html">http://referencesource.microsoft.com/System/R/c50d8ac0eb7bc0d6.html

这是两种方法,当你调用的Process.Start 称为之一。它返回值true或false底部附近注意。假仅返回如果,在启动过程之后,它不能获得的句柄而启动的处理。

That is one of two methods called when you call Process.Start. Notice near the bottom where it returns the value true or false. False is only returned if, after starting the process, it cannot obtain the handle for the process that was started.

为了启动这一进程,它使用 NativeMethods.CreateProcess ,你可以找到这里的源:<一href="http://referencesource.microsoft.com/System/compmod/microsoft/win32/NativeMethods.cs.html#9c52a5ca5f3eeea3">http://referencesource.microsoft.com/System/compmod/microsoft/win32/NativeMethods.cs.html#9c52a5ca5f3eeea3

In order to start the process, it uses NativeMethods.CreateProcess which you can find the source of here: http://referencesource.microsoft.com/System/compmod/microsoft/win32/NativeMethods.cs.html#9c52a5ca5f3eeea3

这仅仅是方法原型 Kernel32.CreateProcess ,该API是在这里找到:<一href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx">https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

Which is just the method prototype for Kernel32.CreateProcess, which the API is found here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

如果你看一下返回值,它说:

If you look at the return values, it says:

如果函数成功,返回值是零。   如果函数失败,返回值是零。

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

我找不到API中何事的CreateProcess ,说如果请求的进程已经运行,也许如果进程无法启动,因为它是一个失败单实例应用程序(如Outlook),那么它可能会失败,但对于多实例应用,如命令提示符,它不应该无法创建一个处理的过程。

I can't find anything in the API for CreateProcess that says it fails if the requested process is already running, perhaps if the process failed to start because it is a single instance application (like Outlook) then it may fail, but for multiple instance applications like the command prompt, it shouldn't fail to create a handle to the process.

所以,说一切之后,有可能是MSDN文档并不完全正确,我没有你的链接,但对于<一个href="https://msdn.microsoft.com/en-us/library/0w4h05yb(v=vs.110).aspx"><$c$c>Process.Start(StartInfo), MSDN说,这对返回值:

So, after saying all that, it is possible that the MSDN documentation is not entirely correct, I don't have the link you have, but for the Process.Start(StartInfo), MSDN says this about the return value:

这与进程的资源,如果没有处理资源启动相关,或为空一个新的进程。 请注意年代开始沿着已经运行同一进程的情况下,一个新的进程将是独立于其他。另外,开始可能会返回一个非空的进程已设置为true的HasExited财产。在这种情况下,启动过程可能启动其自身的现有实例,然后退出。

A new Process that is associated with the process resource, or null if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its HasExited property already set to true. In this case, the started process may have activated an existing instance of itself and then exited.

(重点由我加的)。这并不是说该调用将失败,如果它已经在运行。

(Emphasis added by me). It doesn't say the call will fail if its already running.

有关<一href="https://msdn.microsoft.com/en-us/library/e8zac0ca(v=vs.110).aspx"><$c$c>Process.Start(),它说:

返回值   类型:System.Boolean   如此,如果一个进程的资源启动;假如果没有新的进程资源开始(例如,如果一个现有的过程被重复使用)。

Return Value Type: System.Boolean true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).

所以说如果现有流程重复使用,这是完全由被启动的应用程序或启动它的方法。

So it says if an existing process is reused, this is entirely up to the application being started or the method of starting it.

这篇关于在什么情况下不的的Process.Start()方法返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆