Search code examples
c#javascriptchromium-embeddedcefsharp

CefSharp Executing JavaScript from C#


I'm beginning to mess with CefSharp, but unsure if the behavior I am seeing is avoidable. I am attempting to execute rapid fire JavaScript statement to simulate animations to see if the refresh rate will be good enough for the project use. I've got a grid of some squares, and I would expect that I would see the colors changing rapidly, but all of the changes happen after the loop has finished, with just one refresh instead of many.

    public void start()
    {
        Random random = new Random();

        int x = 0;
        while (x < 5000)
        {
            string num = random.Next(16).ToString();
            var script = String.Format("document.getElementById({0}).style.backgroundColor = colors[Math.floor(Math.random() * (2 - 0 + 1)) + 0];", num);
            ExecuteJavaScript(script);
            //MessageBox.Show("");
            x++;
        }
    }

    private void ExecuteJavaScript(string script)
    {
        this.webView.ExecuteScript(script);
    }

What is going on with ExecuteScript that it is waiting for operations to complete before refreshing?

Thanks in advance for any help!


Solution

  • I've managed to get the desired results by creating a global JavaScript function, and calling the function from ExecuteScript() and in the global JavaScript function, calling start() for a constant loop.