使用类库中的WebBrowser控件

发布于 2024-12-11 02:23:05 字数 1617 浏览 0 评论 0原文

我试图在类库中使用此控件,但是当我运行下面的代码时,我没有看到向 google 发送的请求(使用 fiddler)。

public class WebBrowserTest
{
    public WebBrowserTest()
    {
        var t = new Thread(StartBrowser);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

    private void StartBrowser()
    {
        WebBrowser web;
        web = new WebBrowser();
        web.Navigate("http://www.google.com");
    }
}

我的猜测是这与线程有关,并且可能是线程在控件有机会发送请求之前结束。但我不知道从哪里开始解决这个问题。

解决方案

我发现这个解决方案有效,事件被触发并且主线程等待 STA 线程。

public class WebThread
{
    private WebBrowser web { get; set; }

    public void StartBrowser()
    {
        web = new WebBrowser();
        web.Visible = true;
        web.DocumentCompleted += Web_DocumentCompleted;
        web.ScriptErrorsSuppressed = true;
        web.Navigate("http://www.google.com");

        Application.Run();

        web.Dispose();
    }

    private void Web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        Debug.Print("Arrived: " + e.Url.ToString());

        if (e.Url.ToString() == "http://www.google.com.au/")
        {
            Application.ExitThread();
        }
    }
}


public class WebBrowserTest
{
    public WebBrowserTest()
    {
        Debug.Print("Thread is starting.");
        var webThread = new WebThread();

        var t = new Thread(webThread.StartBrowser);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

        while(t.IsAlive)
        {
            Thread.Sleep(5000);
        }

        Debug.Print("Thread has finished.");
    }
}

I'm trying to use this control inside a class library, but when I run the code below, I did not see a request to google being sent (using fiddler).

public class WebBrowserTest
{
    public WebBrowserTest()
    {
        var t = new Thread(StartBrowser);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

    private void StartBrowser()
    {
        WebBrowser web;
        web = new WebBrowser();
        web.Navigate("http://www.google.com");
    }
}

My guess is this has something to do with threading, and possibly the thread ending before the control gets a chance to send the request. But I have no idea where to start with solving this.

THE SOLUTION

I found this solution to work, the events are getting fired and the main thread waits for the STA thread.

public class WebThread
{
    private WebBrowser web { get; set; }

    public void StartBrowser()
    {
        web = new WebBrowser();
        web.Visible = true;
        web.DocumentCompleted += Web_DocumentCompleted;
        web.ScriptErrorsSuppressed = true;
        web.Navigate("http://www.google.com");

        Application.Run();

        web.Dispose();
    }

    private void Web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        Debug.Print("Arrived: " + e.Url.ToString());

        if (e.Url.ToString() == "http://www.google.com.au/")
        {
            Application.ExitThread();
        }
    }
}


public class WebBrowserTest
{
    public WebBrowserTest()
    {
        Debug.Print("Thread is starting.");
        var webThread = new WebThread();

        var t = new Thread(webThread.StartBrowser);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

        while(t.IsAlive)
        {
            Thread.Sleep(5000);
        }

        Debug.Print("Thread has finished.");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2024-12-18 02:23:05

WebBrowser.Navigate( ... ) 不会阻塞 - 它会在发送请求之前立即返回。由于您的线程函数随后退出,因此整个线程结束并接管您的 WebBrowser 控制权。

如果您只是想下载网页,请查看 WebClient 类。它有许多异步方法,这意味着您可能甚至不必创建自己的线程。

WebBrowser.Navigate( ... ) doesn't block - it returns immediately, before the request is sent. Since your thread function then exits, your whole thread ends and takes your WebBrowser control with it.

If you're just trying to download a web page, have a look at the WebClient class. It has many async methods which means you probably won't even have to create your own thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文