WebBrowser streams don't seem to close

I use WebBrowser to render javascript on web pages to clear the displayed source code, but after several page loads, the CPU consumption reaches 100%, as well as the number of threads. I assume that streams do not close properly after the web page has been rendered. I try to open the browser, extract the source code, and then close the browser and go to the next page. I can get the page displayed, but this program does not make it very far before getting bogged down. I tried adding wb.Stop () but did not help. Memory doesn't seem like a problem (stays at 70% or so). Here is my source code. We appreciate any suggestions. Thank,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Abot.Demo
{
// Threaded version
public class HeadlessBrowser
{
    private static string GeneratedSource { get; set; }
    private static string URL { get; set; }

    public static string GetGeneratedHTML(string url)
    {
        URL = url;

        Thread t = new Thread(new ThreadStart(WebBrowserThread));
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        t.Join();

        return GeneratedSource;
    }

    private static void WebBrowserThread()
    {
        WebBrowser wb = new WebBrowser();
        wb.Navigate(URL);

        wb.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(
                wb_DocumentCompleted);

        while (wb.ReadyState != WebBrowserReadyState.Complete);
            //Application.DoEvents();

        //Added this line, because the final HTML takes a while to show up
        GeneratedSource = wb.Document.Body.InnerHtml;

        wb.Dispose();
        wb.Stop();
    }



    private static void wb_DocumentCompleted(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = (WebBrowser)sender;
        GeneratedSource = wb.Document.Body.InnerHtml;
    }

}

}

+2
1

WebBrowser windows forms. .

, , GUI . , , , , .

, , , winforms, WPF , . . . .

, , , , , .

public static string GetGeneratedHTML(string url)
{
    string result = null;
    ThreadStart pumpMessages = () =>
    {
        EventHandler idleHandler = null;
        idleHandler = (s, e) =>
        {
            Application.Idle -= idleHandler;

            WebBrowser wb = new WebBrowser();
            wb.DocumentCompleted += (s2, e2) =>
            {
                result = wb.Document.Body.InnerHtml;
                wb.Dispose();
                Application.Exit();
            };
            wb.Navigate(url);
        };
        Application.Idle += idleHandler;
        Application.Run();
    };
    if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
        pumpMessages();
    else
    {
        Thread t = new Thread(pumpMessages);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        t.Join();
    }
    return result;
}
+1

Source: https://habr.com/ru/post/1546267/


All Articles