如何将变量分配给 Cefsharp C# 中 void 方法内部的 await

How to assign a variable to await which is inside void method in Cefsharp C#

这是我的:

private async void Button_Click(object sender, EventArgs e)
{
   string jsScript1 = "some java script code";
   await Task.WhenAll(chrome.WaitForLoadAsync(), chrome.EvaluateScriptAsync(jsScript1));

   string jsScript2 = "some java script code";
   await Task.WhenAll(chrome.WaitForLoadAsync(), chrome.EvaluateScriptAsync(jsScript2));

   string jsScript3 = "some java script code";
   await Task.WhenAll(chrome.WaitForLoadAsync(), chrome.EvaluateScriptAsync(jsScript3));
}

如果我想return一个结果并处理它,如何分配一个变量来等待?

我想完成的事情

private async void Button_Click(object sender, EventArgs e)
{
   ...
   ...
   ...
    
   string jsScript4 = "some java script code";
   var task = await Task.WhenAll(chrome.WaitForLoadAsync(), 
   chrome.EvaluateScriptAsync(jsScript4));
   task.ContinueWith(x =>
   {
     if (!x.IsFaulted)
     {
       var response = x.Result;
        if (response.Success == true)
        {
          var final = (List<object>)response.Result;
          foreach (var el in final)
          {
            textHtml.Text += el.ToString() + Environment.NewLine;
          }
        }
     }
   }, TaskScheduler.FromCurrentSynchronizationContext());
}

但我收到错误“无法将 void 分配给隐式类型变量”

我该如何解决这个问题?


使用Thread.Sleep

解决
string jsScript1 = "some java script code";
            await Task.WhenAll(chrome.WaitForLoadAsync(), chrome.EvaluateScriptAsync(jsScript1));

            string jsScript2 = "some java script code";
            await Task.WhenAll(chrome.WaitForLoadAsync(), chrome.EvaluateScriptAsync(jsScript2));

            string jsScript3 = "some java script code";
            await Task.WhenAll(chrome.EvaluateScriptAsync(jsScript3));


//Its important to delete "chrome.WaitForLoadAsync()" look above!
            Thread.Sleep(2000); 
            string jsScript4 = "some java script code";
            var navigationTask = chrome.WaitForLoadAsync();
            var evaluateTask = chrome.EvaluateScriptAsync(jsScript4);
            await Task.WhenAll(navigationTask, evaluateTask, evaluateTask.ContinueWith(x =>
            {
                if (!x.IsFaulted)
                {
                    var response = x.Result;
                    if (response.Success == true)
                    {
                        var final = (List<object>)response.Result;
                        foreach (var el in final)
                        {
                            textHtml.Text += el.ToString() + Environment.NewLine;
                        }
                    }
                }
            }, TaskScheduler.FromCurrentSynchronizationContext()));

如何在不使用 Thread.Sleep 的情况下重写此代码? 我正在使用 chrome.WaitForLoadAsync() 又名“导航任务”,但这与 jsScript4 不一致,可能是因为在 jsScript3 中点击 link 并加载 links,那里没有导航,但在当前页面上加载 links,也许我需要 class 像 chrome.WaitForLoadAsync() 来加载 links/load classes

await Task.WhenAll(...) 的结果无效,因此您应该等待 其他 :)

   string jsScript4 = "some java script code";

   var valueTask = chrome.EvaluateScriptAsync(jsScript4);

   var waitTask = Task.WhenAll(
       chrome.WaitForLoadAsync(),
       valueTask);

   await waitTask.ContinueWith(waitTask =>
   {
     if (!waitTask.IsFaulted)
     {
       var response = valueTask.Result;
        if (response.Success == true)
        {
          var final = (List<object>)response.Result;
          foreach (var el in final)
          {
            textHtml.Text += el.ToString() + Environment.NewLine;
          }
        }
     }
   }, TaskScheduler.FromCurrentSynchronizationContext());

您只需将任务存储为变量并在等待 Task.WhenAll 调用后访问它的结果 属性。

var navigationTask = browser.WaitForLoadAsync();
var evaluateTask = browser.EvaluateScriptAsync(script);

await Task.WhenAll(navigationTask, evaluateTask);

var response = evaluateTask.Result;