An unhandled exception of type "System.InvalidCastException" occurred
2017-04-25 16:51
465 查看
An unhandled exception of type “System.InvalidCastException” occurred in System.Windows.Form.dll
Problem: 在使用Webbrowser时,异步操作完成后,获取Document时报错:An unhandled exception of type “System.InvalidCastException” occurred in System.Windows.Form.dll
Additional information:指定的转换无效。
Description:
Analysis:
这个问题很是莫名其妙,看错误信息让人摸不着头脑;在网上查后才发现是跨线程操作。如何解决?
直接将操作写在当前代码块,而不封装为方法,当有多处需要调用此类操作时,较为繁琐,每次都需重写一遍;
或者将操作封装为方法进行调用,此时需要将方法置于原线程执行,可通过委托实现,见Solution。
Solution:主要是因为跨线程操作导致的问题。
// 需要在创建控件的线程中执行 ... BeginInvoke(new MethodInvoker(delegate () { ClearAll(this.webBrowser1.Document); })); ...
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!
相关文章推荐
- 执行foreach时出现An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll的错误查解
- An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module
- System.InvalidCastException: Unable to cast object of type SqlDataProvider to type DataProvider
- n unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module.Additional information: 未能加载文件或程序集“..
- An unhandled exception of type 'System.StackOverflowException'
- 未处理 System.InvalidCastExceptionMessage="无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 CO
- An exception of type System.Data.SqlClient.SqlException was thrown. Additional exception information: A network-related or instance-specific error occ
- An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more informat
- 报错信息: "System.Web.UI.BaseParser" The type initializer threw an exception.
- AsParallel \AsQueryable<T>().ToList() [System.ArgumentOutOfRangeException was unhandled" 索引超出范围。必须为非负值并小于集合大小]解决方法
- "Exception of type System.ExecutionEngineException was thrown"
- An unhandled exception ('System.EntryPointNotFoundException') Occurred in MsDtsSrvr.exe
- A first chance exception of type 'System.NullReferenceException' occurred发生的时候你确实是遇到了Bug
- An internal error occurred during: "Add Deployment". java.lang.NullPointException 问题解决
- 关于Linq to SQL 的“异常详细信息: System.InvalidCastException: 指定的转换无效。”
- org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named "" must be of type [org.apache.struts.action.Action
- Unable to cast COM object of type 'System.__ComObject' to class type 的解决办法
- System.InvalidCastException: 无法将类型为“DEduSys.SqlServerDal.sysManageUser”的对象强制转换为类型“DEduSys.IDal.IsysManageUser
- GridView输入Excel时出现"System.OutOfMemoryException: Out of memory"及其解决方案。
- System.InvalidCastException异常解决