Process.Start() 在自己的进程上返回 null
我正在使用 Process.Start() 来初始化当前正在运行的应用程序的提升副本。不幸的是 Process.Start() 返回 null,因为它认为它正在使用我的应用程序的现有进程,并且虽然存在现有进程,但它没有指定处理此类入口点的任何方法。
.NET 中是否有任何方法(通过配置或其他方式)可以告诉系统不要重用我的流程的现有副本?此问题似乎仅发生在 Windows XP 上,而不是 Vista 或 7。
代码副本如下:
internal static bool EnsureAssociation()
{
// Check to make sure RoketPack is associated.
if (!Protocol.IsAssociated())
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = UNC.UniversalApplicationPath;
info.UseShellExecute = true;
if (!UAC.IsAdmin())
info.Verb = "runas"; // Provides Run as Administrator
info.Arguments = "--associate";
Process proc = null;
try
{
proc = Process.Start(info);
}
catch (Win32Exception)
{
Errors.Raise(Errors.ErrorType.ERROR_CAN_NOT_ASSOCIATE_PROTOCOL);
return false;
}
if (null != proc)
{
// Wait until the association is complete.
proc.WaitForExit();
return Protocol.IsAssociated();
}
else
{
Errors.Raise(Errors.ErrorType.ERROR_CAN_NOT_ASSOCIATE_PROTOCOL);
return false;
}
}
else
return true;
}
I'm using Process.Start() to initialize an elevated copy of the currently running application. Unfortunately Process.Start() returns null because it thinks it's using an existing process of my application, and while there is an existing process, it doesn't specify any way of handling this kind of entry point.
Is there any way in .NET (via configuration or otherwise) that I can tell the system not to reuse existing copies of my process? This issue only seems to occur on Windows XP, and not Vista or 7.
A copy of the code is below:
internal static bool EnsureAssociation()
{
// Check to make sure RoketPack is associated.
if (!Protocol.IsAssociated())
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = UNC.UniversalApplicationPath;
info.UseShellExecute = true;
if (!UAC.IsAdmin())
info.Verb = "runas"; // Provides Run as Administrator
info.Arguments = "--associate";
Process proc = null;
try
{
proc = Process.Start(info);
}
catch (Win32Exception)
{
Errors.Raise(Errors.ErrorType.ERROR_CAN_NOT_ASSOCIATE_PROTOCOL);
return false;
}
if (null != proc)
{
// Wait until the association is complete.
proc.WaitForExit();
return Protocol.IsAssociated();
}
else
{
Errors.Raise(Errors.ErrorType.ERROR_CAN_NOT_ASSOCIATE_PROTOCOL);
return false;
}
}
else
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过检查 UAC.IsAdmin() 是否为真解决了这个问题,如果是,则只需执行提升的进程将执行的操作即可。
这并不能直接解决 Process.Start() 返回 null 的问题,但我认为造成重用的情况是因为本来要启动的进程在各个方面都是相同的(就好像进程启动时提升了通过“runas”动词,它被认为是不同的并且不会返回 null)。
I solved this by checking to see if UAC.IsAdmin() is true, and if it is, simply perform the actions that the elevated process would have performed.
This doesn't directly solve the issue of Process.Start() returning null, but I think that the re-use situation is caused because the process that would have been started is identical in every way (where as if the process is started elevated via the 'runas' verb, it's considered different and will not return null).
您想要做的是默认行为,即任何程序都可以运行多个实例。防止应用程序运行多次需要额外的代码。
查看 Reflector 的
Process.Start()
您可以跟踪返回
null
的位置。如果进程无法在process.Start()
内启动,它将返回null
。...
你明白了。继续追踪为什么您会得到
null
值。如果您还没有 Reflector 的副本,立即获取!注意:很抱歉,这并没有为您的问题提供准确解决方案,但它表明您可以自己找到它。 :)
哈,
What you want to do is the default behaviour, i.e., any program can run multiple instances. Preventing the application from running more than once would require additional code.
Looking in Reflector at
Process.Start()
You can trace where is returning
null
. It will returnnull
if the process fails to start inside ofprocess.Start()
....
You get the idea. Continue to trace why you are getting a
null
value. If you haven't got a copy of Reflector, GET IT NOW!NB: Apologies that this does not give you the exact solution to your problem, however it shows you can go about finding it yourself. :)
HTH,