Home > Software design >  How to assign a variable to await which is inside void method in Cefsharp C#
How to assign a variable to await which is inside void method in Cefsharp C#

Time:05-01

This is what I have:

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));
}

How to assign a variable to await if i want to return a result and process it?

What I would like to accomplish

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());
}

But Im getting the error "Cannot assign void to an implicitly-typed variable"

How can i solve this problem?


Solution by using 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()));

How to rewrite this code without using Thread.Sleep? Im using chrome.WaitForLoadAsync() aka "navigation task" but this does not work at line with jsScript4, maybe because in jsScript3 clicks on link with load links, there is no navigation there, but loads links on current page, maybe i need class like chrome.WaitForLoadAsync() for load links/load classes

CodePudding user response:

You simply store the task as a variable and access it's Result property after the await Task.WhenAll call.

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

await Task.WhenAll(navigationTask, evaluateTask);

var response = evaluateTask.Result;

CodePudding user response:

result of await Task.WhenAll(...) is void, so you should await something else :)

   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());
  • Related