How to create and use WebBrowser in background thread?

前端 未结 1 980
感动是毒
感动是毒 2021-01-28 23:38

How can I create System.Windows.Forms.WebBrowser in background STA thread? I try use some code like this:

var tr = new Thread(wbThread);
tr.SetApartmentState(Ap         


        
1条回答
  •  情歌与酒
    2021-01-29 00:29

    Why don't you use the default WebBrowser control like this?

    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(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.
    }
    

    Edit: You are using old System.Windows.Forms.WebBrowser control, instead System.Windows.Controls.WebBrowser which is part of WPF.

    0 讨论(0)
提交回复
热议问题