Waiting for WebBrowser ajax content

前端 未结 4 1302
感动是毒
感动是毒 2021-02-14 14:21

I want to pause the execution of my thread until a particular div has been loaded via ajax into a WebBrowser instance. Obviously I can continuously check for the presence of thi

4条回答
  •  自闭症患者
    2021-02-14 14:35

    You should not call Thread.Sleep as it will block the UI thread.

    A better solution is to create an asynchronous task. Inside this task you can call Task.Delay which won't interfere with the UI.

    static async Task WaitForElement(WebBrowser browser, string elementID, TimeSpan timeout)
    {
        long startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
        var timeoutMS = timeout.TotalMilliseconds;
    
        // While the timeout has not passed...
        while (DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime < timeoutMS)
        {
            // Wait for 200ms
            await Task.Delay(TimeSpan.FromMilliseconds(200));
    
            // Check if the document contains the element
            var document = (HTMLDocument) browser.Document;
            var element = document.getElementById(elementID);
            if (element != null)
            {
                // Element found, stop looping
                return element;
            }
        }
    
        throw new Exception($"Element was not loaded after {timeoutMS} milliseconds");
    }
    

    The code above checks the DOM every 200 milliseconds to see if the element with the given ID exists. It also contains a timeout (e.g. 10 seconds) in case the element never gets loaded for any unexpected reason.

    Here is an example showing how to use this function to read the value out of a text field added to the document after an AJAX call:

    var input = (IHTMLInputElement) await WaitForElement(myBrowserControl, "input-id", TimeSpan.FromSeconds(10));
    var value = input.value; // Read the value of the input field
    

提交回复
热议问题