TransWikia.com

Web Brower is not responding when looping a javascript through while loop

Stack Overflow Asked by Renato Lulic on December 1, 2020

I have an issue where my web browser is not responding when looping javascript through a while loop, so I can’t cancel this loop or do anything with my web browser.

I’m triggering javascript with checkbox and I can’t uncheck the checkbox after I checked it because my Browser is not responding while looping

Here is the code:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        while (checkBox1.Checked == true)
        {
            System.Threading.Thread.Sleep(1000);
            CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("var elems = document.getElementsByClassName('VotingButton VotingButton--upvote btn-white'); elems[0].click();");
            System.Threading.Thread.Sleep(1000);
            CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("var elems1 = document.getElementsByClassName('task-skip-submit js-next-question'); elems1[0].click();");
        }
    }

One Answer

I'm not very familiar with cefsharp, but from the C# side of things, unless the method is async, a Thread.Sleep still won't relinquish control of the UI loop back to the front-end. Maybe that's not the case, but have you tried turning checkBox1_CheckChanged into an async Task?

If that's not an option, then I think you need to kick the while loop into a background thread. Do you have an async context you an use?

Task.Run(() => {
  while (checkBox1.Checked == true)
        {
            System.Threading.Thread.Sleep(1000);
            CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("var elems = document.getElementsByClassName('VotingButton VotingButton--upvote btn-white'); elems[0].click();");
            System.Threading.Thread.Sleep(1000);
            CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("var elems1 = document.getElementsByClassName('task-skip-submit js-next-question'); elems1[0].click();");
        }
});

Correct answer by Peter Dowdy on December 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP