WPF复制异常问题(OpenClipboard 失败 (异常来自 HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN)))
2017-01-02 18:41
218 查看
最近在维护WPF系统的时候发现的问题,刚刚开始自己的电脑都不能重现,后面写日志跟踪才发现问题的所在。问题主要是由于:1.在程序访问剪切板的时候,有其他程序正在占用剪切板,导致自己的程序无法访问,从而抛出异常;2.没有访问的权限,导致自己的程序无法访问。这个是之前在App.xaml.cs文件中的写法:
#region
///// <summary>
///// 处理异常的方法
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
//在wpf中如果复制没有权限此时也会进来这里(PS:以管理员和兼容性运行此程序即可哦)
if (e.Exception.InnerException.Message.Contains("网络") || e.Exception.InnerException.Message.Contains("超时") || e.Exception.InnerException.Message.Contains("暂停"))
{
BugReport report = new BugReport(e.Exception);
report.Save();
e.Handled = true;
report.LaunchBugReport();
MessageBox.Show(e.Exception.InnerException.Message);
}
else
{
BugReport report = new BugReport(e.Exception);
report.Save();
report.LaunchBugReport();
e.Handled = true;
Application.Current.Shutdown();
}
}
这个是修改之后的写法:
在App.xaml文件中添加下面代码中红色的部分
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
在App.xaml.cs文件中添加代码:
private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
var comException = e.Exception as System.Runtime.InteropServices.COMException;
if (comException != null && comException.ErrorCode == -2147221040)
e.Handled = true;
BugReport report = new BugReport(e.Exception);
report.Save();
e.Handled = true;
}
这种方法中剪切板动作会自动多次尝试,由于抛出的异常被App中的异常处理给截获了,所以会不断的尝试直到成功。(PS:以上只是个人的看法,有不当之处,烦请指正。谢谢!)
#region
///// <summary>
///// 处理异常的方法
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
//在wpf中如果复制没有权限此时也会进来这里(PS:以管理员和兼容性运行此程序即可哦)
if (e.Exception.InnerException.Message.Contains("网络") || e.Exception.InnerException.Message.Contains("超时") || e.Exception.InnerException.Message.Contains("暂停"))
{
BugReport report = new BugReport(e.Exception);
report.Save();
e.Handled = true;
report.LaunchBugReport();
MessageBox.Show(e.Exception.InnerException.Message);
}
else
{
BugReport report = new BugReport(e.Exception);
report.Save();
report.LaunchBugReport();
e.Handled = true;
Application.Current.Shutdown();
}
}
这个是修改之后的写法:
在App.xaml文件中添加下面代码中红色的部分
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
在App.xaml.cs文件中添加代码:
private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
var comException = e.Exception as System.Runtime.InteropServices.COMException;
if (comException != null && comException.ErrorCode == -2147221040)
e.Handled = true;
BugReport report = new BugReport(e.Exception);
report.Save();
e.Handled = true;
}
这种方法中剪切板动作会自动多次尝试,由于抛出的异常被App中的异常处理给截获了,所以会不断的尝试直到成功。(PS:以上只是个人的看法,有不当之处,烦请指正。谢谢!)
相关文章推荐
- WPF复制异常问题(OpenClipboard 失败 (异常来自 HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN)))
- WPF剪切板问题-OpenClipboard HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN))
- IIS中使用Microsoft.Office.Interop.Excel 常见问题:RPC 服务器不可用。 (异常来自 HRESULT:0x800706BA) 的异常。等
- 拒绝访问。 (异常来自 HRESULT:0x80070005 (E_ACCESSDENIED)) 使用Enterprise Library 3.0的磁盘缓存Isolated Storage 的问题
- IDE问题 "被呼叫方拒绝接收呼叫。 (异常来自 HRESULT:0x80010001 (RPC_E_CALL_REJECTED))"
- DLL错误之——无法加载DLL"***.dll":找不到指定的模块(异常来自HRESULT:0x8007007E)问题的终极感悟
- 库没有注册 (异常来自异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY )
- 检索 COM 类工厂中 CLSID 为 {28E68F9A-8D75-11D1-8DC3-3C302A000000} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))
- 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。 (异常来自 HRESULT:0x80070005 (E_ACCESSDENIED))。
- vs2015 系统找不到指定的文件(异常来自HRESULT:0x80070002)问题的解决方法
- ASP.NET生成excel失败(异常来自 HRESULT:0x800A03EC )
- 检索 COM 类工厂中 CLSID 为 {820280E0-8ADA-4582-A1D9-960A83CE8BB5} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。
- C#开发中提示无法加载DLL"***.dll":找不到指定的模块(异常来自HRESULT:0x8007007E)问题的解决办法
- 检索 COM 类工厂中 CLSID 为 {10021F00-E260-11CF-AE68-00AA004A34D5} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。
- 关于无法加载DLL"***.dll":找不到指定的模块(异常来自HRESULT:0x8007007E)问题的解决办法
- IDE问题 "被呼叫方拒绝接收呼叫。 (异常来自 HRESULT:0x80010001 (RPC_E_CALL_REJECTED))"
- 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。 (异常来自 HRESULT:0x8000401A)。
- 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。 (异常来自 HRESULT:0x8000401A)。 在 BatchImportEntryTable.GetExcelData(String FileName)
- PDFLibnet.dll由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。 (异常来自 HRESULT:0x800736B1)
- (word导出问题)解决:服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))的解决方法