Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 抛出System.Runtime.InteropService.COMException并关闭Internet Explorer_C#_Shdocvw.internetexplorer - Fatal编程技术网

C# 抛出System.Runtime.InteropService.COMException并关闭Internet Explorer

C# 抛出System.Runtime.InteropService.COMException并关闭Internet Explorer,c#,shdocvw.internetexplorer,C#,Shdocvw.internetexplorer,我正在使用SHDocVw.InternetExplorer打开并控制一个浏览器窗口,以便在内部网站上实现自动化。软件通常按需要运行,但浏览器偶尔会关闭。我已经找到了导致这种情况发生的代码行,但是我想不出解决问题的方法 我的原始代码如下所示: private InternetExplorer window; public HTMLDocument Document { get { try { return (HTMLD

我正在使用
SHDocVw.InternetExplorer
打开并控制一个浏览器窗口,以便在内部网站上实现自动化。软件通常按需要运行,但浏览器偶尔会关闭。我已经找到了导致这种情况发生的代码行,但是我想不出解决问题的方法

我的原始代码如下所示:

private InternetExplorer window;

public HTMLDocument Document 
{ 
    get 
    {
        try
        {
            return (HTMLDocument)window.Document;
        }
        catch (System.Runtime.InteropServices.COMException)
        {
            return null;
        }
    } 
}
但是,每当调用
window.Document
导致
COMException
时,浏览器就会关闭。我尝试创建一个isReady布尔值,当我尝试导航离开当前页面时设置为false,当触发
NavigateComplete2
事件时设置为true,但如果浏览器询问“您确定要离开此页面吗?”这意味着我无法与这些弹出窗口交互,则不会触发

因此,我尝试将代码更改为:

private InternetExplorer window;

public HTMLDocument Document 
{ 
    get 
    {
        try
        {
            if (window.Busy) return null;
            return (HTMLDocument)window.Document;
        }
        catch (System.Runtime.InteropServices.COMException)
        {
            return null;
        }
    } 
}
虽然我经历了较少的崩溃,但当
窗口中出现
COMException
异常时,它仍会偶尔关闭浏览器。Busy
呼叫

由于
NavigateComplete2
在浏览器和
InternetExplorer中断导航时不会触发。繁忙的
也会导致浏览器关闭,因此我不知道如何解决

编辑: 我现在也尝试过:
window.ReadyState==tagREADYSTATE.ReadyState\u COMPLETE
但它在
window.ReadyState
上抛出相同的
COMException
,并使窗口崩溃