如何在后台线程中创建和使用WebBrowser?

时间:2011-12-27 14:50:27

标签: wpf multithreading browser

如何在后台STA线程中创建System.Windows.Forms.WebBrowser?我尝试使用这样的代码:

var tr = new Thread(wbThread);
tr.SetApartmentState(ApartmentState.STA);
tr.Start();

private void wbThread()
{
     CWebBrowser browser = new CWebBrowser();
     var text = browser.Navigate("http://site.com", CWebBrowser.EventType.loadCompleted).Body.InnerHtml;
}

CWebBrowser - 自定义类,委托System.Windows.Forms.WebBrowser对象导航方法并等到页面完成加载。问题是System.Windows.Forms.WebBrowser对象上的LoadCompleted事件永远不会引发。我找到了一些解决方案here,但它不起作用(在我的WPF应用程序上找不到方法Application.Run())。

public class CWebBrowser : ContentControl
{
    public readonly System.Windows.Forms.WebBrowser innerWebBrowser;
    private readonly AutoResetEvent loadCompletedEvent;
    private readonly AutoResetEvent navigatedEvent;

    public enum EventType
    {
        navigated, loadCompleted
    }

    public CWebBrowser()
    {
        innerWebBrowser = new System.Windows.Forms.WebBrowser();
        loadCompletedEvent = new AutoResetEvent(false);
        navigatedEvent = new AutoResetEvent(false);

        System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
        host.Child = innerWebBrowser;
        Content = host;

        innerWebBrowser.DocumentCompleted +=new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(innerWebBrowser_DocumentCompleted);
        innerWebBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(innerWebBrowser_Navigated);
    }

    void innerWebBrowser_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e)
    {
        navigatedEvent.Set();
    }

    void innerWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
    {
        if (((sender as System.Windows.Forms.WebBrowser).ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) || innerWebBrowser.IsBusy)
            return;

        var doc = innerWebBrowser.Document;
        loadCompletedEvent.Set();
    }

    public System.Windows.Forms.HtmlDocument Navigate(string url, EventType etype)
    {
        if (etype == EventType.loadCompleted)
            loadCompletedEvent.Reset();
        else if (etype == EventType.navigated)
            navigatedEvent.Reset();

        innerWebBrowser.Navigate(url);

        if (etype == EventType.loadCompleted)
            loadCompletedEvent.WaitOne();
        else if (etype == EventType.navigated)
            navigatedEvent.WaitOne();

        System.Windows.Forms.HtmlDocument doc = null;
        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(
            delegate
            {
                doc = innerWebBrowser.Document;
            }));

        return doc;
    }
}

Thansk提出所有建议并抱歉我的英语不好:o(

1 个答案:

答案 0 :(得分:0)

为什么不使用这样的默认WebBrowser控件?

public MainPage()
{
    InitializeComponent();

    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(startNavigate);
}

void startNavigate()
{
    WebBrowser wb = new WebBrowser();
    wb.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);
    wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(wb_Navigated);
    wb.Navigate(new Uri("http://www.google.com"));
}

void wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    // e.Content
}

void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
    // e.Content when the document finished loading.
}

修改:您正在使用旧的System.Windows.Forms.WebBrowser控件,而不是System.Windows.Controls.WebBrowser,它是WPF的一部分。