AnswerBun.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!

Related Questions

Update employee manager using employeeid

1  Asked on December 19, 2020 by user8624315

   

Skipping the first step in a loop for the first iteration

2  Asked on December 19, 2020 by qua

     

Make a circular button programatically

3  Asked on December 19, 2020 by user14469311

         

How to delete enclosing braces in Vim?

4  Asked on December 19, 2020 by robottinosino

 

How to swap my list element’s name and value in R?

3  Asked on December 19, 2020 by achai

   

How can we sum the values group by from file using shell script

2  Asked on December 18, 2020 by kashif

       

How to correlate scalar values of two pandas dataframes

2  Asked on December 18, 2020 by kaleb-coberly

         

Handling arrays of hours

1  Asked on December 18, 2020 by matheus-alves

 

How can I have a C++ set with more than 1 data type?

1  Asked on December 18, 2020 by blakcy88

     

Laravel Auth Guard Users From Array

0  Asked on December 18, 2020 by gstar

   

How can if/elif/else be replaced with min() and max()?

3  Asked on December 18, 2020 by nsmedira

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir