Skip to content

EvaluateScriptAsync won't return second time #5155

@zangetsudo

Description

@zangetsudo

Is there an existing issue for this?

  • I have searched both open/closed issues, no issue already exists.

CefSharp Version

138.0.170

Operating System

Windows 10

Architecture

x64

.Net Version

net fwk 4.7.2

Implementation

OffScreen

Reproduction Steps

run it every time

Expected behavior

run as normal

Actual behavior

run it every time, it will blocked and won't return.
(
code: await Task.WhenAll(tcs.Task);
function : InjectScriptAndRun
)

Regression?

dont konw if it works before.

Known Workarounds

No response

Does this problem also occur in the CEF Sample Application

Yes using WPF/OffScreen command line args

Other information

    public static void LoadPageAsync(ChromiumWebBrowser browser)
    {
        var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
        EventHandler<LoadingStateChangedEventArgs> handler = null;
        handler = (sender, args) =>
        {
            if (!args.IsLoading)
            {
                browser.LoadingStateChanged -= handler;                    
                tcs.TrySetResult(true);
            }
        };
        browser.LoadingStateChanged += handler;
        var r = Task.WhenAll(tcs.Task);
        r.Wait();
    }

    private static async Task InjectScriptAndRun(ChromiumWebBrowser browser, string js, string shotName)
    {
        var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
        EventHandler<LoadingStateChangedEventArgs> handler = null;
        handler = (s, e) =>
        {
            if (!e.IsLoading)
            {
                Thread.Sleep(500);
                var scriptTask = browser.EvaluateScriptAsync(js);                    
                scriptTask.ContinueWith(t =>
                {
                    if (!t.Result.Success)
                    {
                        tcs.TrySetException(new Exception("EvaluateScriptAsync failed:" + t.Result.Message));                            
                    }
                    else
                    {
                        Thread.Sleep(500);
                        var task = browser.CaptureScreenshotAsync();
                        task.ContinueWith(x =>
                        {
                            var screenshotPath = Path.Combine(myPath, $"CefSharp screenshot {shotName}.png");
                            var bitmapAsByteArray = x.Result;
                            File.WriteAllBytes(screenshotPath, bitmapAsByteArray);
                            Process.Start(new ProcessStartInfo(screenshotPath)
                            {
                                UseShellExecute = true
                            });
                        }, TaskScheduler.Default);
                        tcs.TrySetResult(true);
                    }
                    browser.LoadingStateChanged -= handler;
                });

            }
        };
        browser.LoadingStateChanged += handler;
        await Task.WhenAll(tcs.Task);
    }
    
    
    main:
    

      const string testUrl = "https://aaa.com";
      const string issueUrl = "https://bbb.com";
      
      var settings = new CefSettings()
      {
          CachePath = Path.Combine(myPath, "CefSharp")
      };

      Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
      var browser = new ChromiumWebBrowser(testUrl);

      var t1 = InjectScriptAndRun(browser, js, "login");
      Task.WaitAll(t1);

      browser.LoadUrl(issueUrl);
      LoadPageAsync(browser);

      Thread.Sleep(500);
      var t2 = InjectScriptAndRun(browser, ei, "issue");

      Task.WaitAll(t2);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions