在使用 “using mshtml;”的过程中出现了这个问题
类型于:
public static HTMLDocumentClass getCurrentInterlibHtmlPage()
{
HTMLDocumentClass htmlDoc = IeCurWin.getCurrentWindowPage();
if (htmlDoc == null) return null;
String pageUrl = ToolUtil.trimStringNull(htmlDoc.url);
String titleStr = ToolUtil.trimStringNull(htmlDoc.title);
//MessageBox.Show(titleStr);
if (titleStr.StartsWith("流通管理 -- 3RD Auto System Interlib") && pageUrl.EndsWith("maxLoanMain.jsp"))
{
HTMLDocument htmlDocument = (HTMLDocument)htmlDoc;
IHTMLElement iframeElement = htmlDocument.getElementById("mainFrame");
/*MessageBox.Show("[------Test 1-------] " + iframeElement.outerHTML);
IHTMLFrameBase2 frameBase2 = (IHTMLFrameBase2)iframeElement;
if (frameBase2.readyState.Equals("complete", StringComparison.OrdinalIgnoreCase))
{
//这一行一执行 frameBase2.contentWindow 就已经抛出异常,
//异常信息为:System.InvalidCastException : Specified cast is not valid.
MessageBox.Show("[------Test 2-------] " + (frameBase2.contentWindow == null ? "yes" : "no"));
}*/
//表示带帧的
mshtml.IHTMLFrameBase2 iFrame = (mshtml.IHTMLFrameBase2)htmlDoc.all.item("mainFrame", 0);
mshtml.IHTMLDocument2 iHTMLDocument2 = iFrame.contentWindow.document;
htmlDoc = (HTMLDocumentClass)iHTMLDocument2;
}
return htmlDoc;
}
百度查找是否有其他人员遇到相关问题,查找到
<a href="https://bbs.csdn.net/topics/390820111">https://bbs.csdn.net/topics/390820111</a>但是这个问题居然没解决,很绝望。
从新过一遍代码,看看是哪里的问题:发现我是在线程中调用的。
搞了半天后,终于找到了解决方法,加上invoke就可以解决问题了:
this.Invoke(new MethodInvoker(delegate
{
}));