U
U
Uncle Bogdan2021-06-05 17:54:26
Parsing
Uncle Bogdan, 2021-06-05 17:54:26

Weird errors when running the HtmlAgilityPack.HtmlDocument.LoadHtml method. What to do?

Some terrible errors when running the HtmlAgilityPack.HtmlDocument.LoadHtml function.

Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
Or
Unexpected error.

It works, it doesn't work.

What's the catch?

The code:

public partial class Form1 : Form
    {
        private ChromiumWebBrowser browser;
        private string html;
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        HtmlNodeCollection node;

        public Form1()
        {
            InitializeComponent();
            InitializeChromium();

            browser.FrameLoadEnd += WebBrowserFrameLoadEnded;
        }

        private async Task<string> GetHTMLFromWebBrowser()
        {
            string TaskHtml = await browser.GetBrowser().MainFrame.GetSourceAsync().ConfigureAwait(false); // cefsharp браузер дает html, не важно.
            return TaskHtml;
        }


        private async void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
        {
            html = await GetHTMLFromWebBrowser();
  
            if(html != null)
            {
                doc.LoadHtml(html);
                // дальше доходит редко, В 1 из 5 почему-то срабатывает.
             }
        } 
       }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-06-05
@motkot

You cannot access it from two threads at the same time.
Wrap in a lock

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question