【问题标题】:WebBrowser control throws seemingly random NullReferenceExceptionWebBrowser 控件抛出看似随机的 NullReferenceException
【发布时间】:2013-04-18 01:38:56
【问题描述】:

这几天我正在开发一个基于 WebBrowser 的网络爬虫。在使用了几个使用 Threads 和 DocumentCompleted 事件的原型之后,我决定尝试看看是否可以制作一个简单易懂的 Webscraper。

目标是创建一个不涉及实际 Thread 对象的 Webscraper。我希望它按顺序工作(即转到 url、执行操作、转到其他 url 等)。

这是我目前得到的:

public static class Webscraper
{
    private static WebBrowser _wb;
    public static string URL;

    //WebBrowser objects have to run in Single Thread Appartment for some reason.
    [STAThread] 
    public static void Init_Browser()
    { 
        _wb = new WebBrowser();
    }


    public static void Navigate_And_Wait(string url)
    {
        //Navigate to a specific url.
        _wb.Navigate(url);

        //Wait till the url is loaded.
        while (_wb.IsBusy) ;

        //Loop until current url == target url. (In case a website loads urls in steps)
        while (!_wb.Url.ToString().Contains(url))
        {
            //Wait till next url is loaded
            while (_wb.IsBusy) ;
        }

        //Place URL
        URL = _wb.Url.ToString();
    }
}

我是一个新手程序员,但我认为这是非常简单的代码。 这就是为什么我讨厌程序出于某种原因在这段代码中抛出 NullReferenceException 的原因:

 _wb.Url.ToString().Contains(url)

我刚刚调用了 _wb.Navigate() 方法,因此 NullReference 不能在 _wb 对象本身中。所以我唯一能想象的是 _wb.Url 对象是空的。但是 while _wb.IsBusy() 循环应该可以防止这种情况发生。

那么发生了什么,我该如何解决?

【问题讨论】:

    标签: c# winforms exception-handling webbrowser-control nullreferenceexception


    【解决方案1】:

    不建议在 UI 线程上忙于等待 (while (_wb.IsBusy) ;)。如果您使用 .Net 4.5 的新功能async/await,您可以获得类似的效果(即转到 url、执行操作、转到其他 url 等等。)

    public static class SOExtensions
    {
        public static Task NavigateAsync(this WebBrowser wb, string url)
        {
            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
            WebBrowserDocumentCompletedEventHandler completedEvent = null;
            completedEvent = (sender, e) =>
            {
                wb.DocumentCompleted -= completedEvent;
                tcs.SetResult(null);
            };
            wb.DocumentCompleted += completedEvent;
    
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);
    
            return tcs.Task;
        }
    }
    
    
    
    async void ProcessButtonClick()
    {
        await webBrowser1.NavigateAsync("http://www.stackoverflow.com");
        MessageBox.Show(webBrowser1.DocumentTitle);
    
        await webBrowser1.NavigateAsync("http://www.google.com");
        MessageBox.Show(webBrowser1.DocumentTitle);
    }
    

    【讨论】:

    • 在我使用 .Net 4.0 的那一刻,但这看起来正是我需要的东西。
    猜你喜欢
    • 2016-11-19
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多