Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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# 大量使用Watin(保持开放的问题)_C#_Asp.net_Watin - Fatal编程技术网

C# 大量使用Watin(保持开放的问题)

C# 大量使用Watin(保持开放的问题),c#,asp.net,watin,C#,Asp.net,Watin,我在C#console应用程序中使用Watin来抓取网站,有五个控制台应用程序同时运行。 我之所以部分使用Watin作为爬虫程序,是因为一些网站使用javascript(或ajax)来设置页面内容 以下是获取页面注释计数的示例代码: Settings.Instance.MakeNewIeInstanceVisible = false; using (var browser = new IE(commentLink, true)) {

我在C#console应用程序中使用Watin来抓取网站,有五个控制台应用程序同时运行。 我之所以部分使用Watin作为爬虫程序,是因为一些网站使用javascript(或ajax)来设置页面内容

以下是获取页面注释计数的示例代码:

        Settings.Instance.MakeNewIeInstanceVisible = false;
        using (var browser = new IE(commentLink, true))
        {
            browser.Link(Find.ByUrl(commentLink)).WaitUntilExists(20);

            Span commentSpan = browser.Span("COUNT_TOTAL");

            if (commentSpan.Exists)
            {
                int commentCount;
                if (Int32.TryParse(commentSpan.InnerHtml, out commentCount))
                {
                    return commentCount;
                }
            }
        }
我的问题是,在运行这5个控制台应用程序一段时间(90分钟)后,很多IE实例都保持打开状态(因为超时或错误,或者IE忙),因此系统速度非常慢,需要重新启动


我如何更改代码以防止这种情况发生并使我的应用程序保持高效?

我认为,在您的示例代码中只有一个时刻,IE不会关闭。由于您在使用的
内部,所以即使在使用
内部发生异常,浏览器也将被处理,所以一切正常

但在创建浏览器时:

new IE(commentLink, true)
您不在使用
内部,也没有神奇的
尝试…捕获
。 试试这个,如果有帮助,请告诉我:

Settings.Instance.MakeNewIeInstanceVisible = false;
using (var browser = new IE(true))
{
    browser.GoTo(commentLink);

    browser.Link(Find.ByUrl(commentLink)).WaitUntilExists(20);

    Span commentSpan = browser.Span("COUNT_TOTAL");

    if (commentSpan.Exists)
    {
        int commentCount;
        if (Int32.TryParse(commentSpan.InnerHtml, out commentCount))
        {
            return commentCount;
        }
    }
}