当 UAC 被拒绝时,Process.Start 永远不会返回 [英] Process.Start never returns when UAC denied

查看:35
本文介绍了当 UAC 被拒绝时,Process.Start 永远不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更新程序 exe,用于关闭主 exe,将其替换为更新的 exe,然后启动该更新的 exe.当更新程序尝试启动更新的 exe 时,如果用户拒绝了 UAC 权限对话框,则更新程序将挂起.这是因为 Process.Start() 函数永远不会返回.顺便说一句,我的 CPU 周期表显示几乎没有使用.

I have an updater exe that is meant to close the primary exe, replace it with an updated exe, and then launch that updated exe. When the updater attempts to start the updated exe, if the UAC permissions dialog is denied by the user, the updater will hang. This is because the Process.Start() function never returns. My CPU cycles meter indicates practically no usage btw.

我希望我的所有用户都对 UAC 说是",但既然我在这里,我想至少用某种错误消息来处理这种情况.假设我的用户至少有 Windows 7.exes 本身是 32 位 Winforms 应用程序.目标 .Net 框架是 4.0.使用 Visual Studio 2010 Ultimate.

I would hope all my users just say "yes" to the UAC, but since I'm here I'd like to handle this case with some kind of error message at least. Assume my users will have at least Windows 7. The exes themselves are 32 bit Winforms applications. Targeted .Net Framework is 4.0. Using Visual Studio 2010 Ultimate.

关于如何检测我的用户何时拒绝 UAC 对话的任何想法?

Any ideas on how to detect for when my user declines the UAC dialog?

我猜我能做的就是让 Process.Start() 在一个单独的线程上运行,该线程将在一段时间后超时.更多代码:

I'm guessing all I can do is make the Process.Start() run on a separate thread that will timeout after a while. For more code:

private void RestartProcess()
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Users\Me\Documents\Visual Studio 2010\Projects\updated.exe";
    MessageBox.Show("Attempting to start process");
    Process newProc = Process.Start(startInfo);
    MessageBox.Show("If this shows, the user has clicked YES in the UAC.");
}

解决方案:

Process.Start() 以 Win32Exception 静默退出,除非使用 Try{}Catch{} 块来捕获错误.

Process.Start()exits silently with a Win32Exception unless one uses a Try{}Catch{} block to catch the error.

推荐答案

   Process newProc = Process.Start(startInfo);
   MessageBox.Show("If this shows, the user has clicked YES in the UAC.");

这是正常的,Process.Start() 引发的异常将绕过 MessageBox.Show() 调用.对于 Windows 错误代码 1223,ERROR_CANCELLED,操作已被用户取消"是一个 Win32Exception.

This is normal, the exception that's raised by Process.Start() will bypass the MessageBox.Show() call. It is a Win32Exception for Windows error code 1223, ERROR_CANCELLED, "The operation was cancelled by the user".

很明显,您希望避免在这里吞下异常.

Clearly you'll want to avoid swallowing exceptions here.

这篇关于当 UAC 被拒绝时,Process.Start 永远不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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