Run and control browser control in different thread

前端 未结 1 1970
眼角桃花
眼角桃花 2020-12-07 04:52

I have my main gui class with some subclassess. There are +- 3 threads that are collecting data from various internet sources and API gateways etc.

Now, out of one o

1条回答
  •  太阳男子
    2020-12-07 05:48

    This should work

    var th = new Thread(() =>
    {
        WebBrowserDocumentCompletedEventHandler completed = null;
    
        using (WebBrowser wb = new WebBrowser())
        {
            completed = (sndr, e) =>
            {
                //Do Some work
    
                wb.DocumentCompleted -= completed;
                Application.ExitThread();
            };
    
            wb.DocumentCompleted += completed;
            wb.Navigate(url);
            Application.Run();
        }
    });
    
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
    th.Join();
    

    that said, I would use WebClient or HttpWebRequest together with HtmlAgilityPack to download and parse html resources

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