简体   繁体   English

从非异步方法调用异步方法

[英]Calling async method from non async method

Here is my code in doing so but it seems like I could not do other things once it started calling the async function and then the app will not respond. 这是我的代码,但是一旦它开始调用异步函数,然后应用程序将不响应,我似乎无法做其他事情。 I would like to just run it to the background. 我想把它运行到后台。

I'm doing a search and in every 3 letters, it will call the api to get datas if have match. 我正在进行搜索,并且在每3个字母中,如果匹配,它将调用api获取数据。 Once I have input 3 letters, it then it calls to the API and I could not input more letters because the app is not responding. 一旦我输入3个字母,然后它调用API,我无法输入更多的字母,因为应用程序没有响应。

How to call the async function and will just run in the background so that I could still search. 如何调用异步函数,只在后台运行,以便我仍然可以搜索。

void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
     var newText = e.NewTextValue;
     //once 3 keystroke is visible by 3
     if (newText.Length % 3 == 0)
     {
          //Call the web services
          var result = GettingModel(newText);
          if (result != null || result != string.Empty)
          {
               ModelVIN.Text = result;
          }    
     }
}

private string GettingModel(string vinText)
{
      var task = getModelForVIN(vinText);
      var result = task.Result;    
      return result.Model;
}

private async Task<VINLookUp> getModelForVIN(string vinText)
{
      var deviceId = CrossDeviceInfo.Current.Model;
      deviceId = deviceId.Replace(" ", "");
      var requestMgr = new RequestManager(deviceId);

      var VinData = new VINLookUp();    
      VinData = await requestMgr.getModelForVIN(vinText);    
      return VinData;
}

Thanks in advance for the help. 先谢谢您的帮助。

You don't need the GettingModel(string vinText) method. 您不需要GettingModel(string vinText)方法。 By calling the Task.Result you are blocking the main thread. 通过调用Task.Result您将阻止主线程。

Calling .Result in the UI thread will likely deadlock everything which is what you are experiencing. 在UI线程中调用.Result可能会使您遇到的所有事情陷入僵局。 Use ContinueWith or async void with await . 使用ContinueWithasync void await

You can make your Entry_TextChanged async and await the web request so that it doesn't block the UI. 您可以使Entry_TextChanged异步并await Web请求,以便它不会阻止UI。

You can even run it on a separate thread and use ContinueWith() if you don't require to make the make user wait for the operation to complete. 您甚至可以在单独的线程上运行它并使用ContinueWith()如果您不需要让make用户等待操作完成。 If you are going that route make sure you use Device.BeginInvookeOnMainThread() to run any code that needs to be run on UI thread. 如果你要去那条路线,请确保使用Device.BeginInvookeOnMainThread()来运行需要在UI线程上运行的任何代码。

The better code would be : 更好的代码是:

private async void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
     var newText = e.NewTextValue;
     //once 3 keystroke is visible by 3
     if (newText.Length % 3 == 0)
     {
          //Call the web services
          var result = await GetModelStringForVIN(newText);
          if (string.IsNullOrEmpty(result) == false)
          {
               ModelVIN.Text = result;
          }    
     }
} 

private async Task<string> GetModelStringForVIN(string vinText)
{
      var deviceId = CrossDeviceInfo.Current.Model;
      deviceId = deviceId.Replace(" ", string.Empty);
      var requestMgr = new RequestManager(deviceId);

      var VinData = await requestMgr.getModelForVIN(vinText);

      return VinData?.Model;
 }

The following links would help you understand the concepts better : 以下链接可帮助您更好地理解概念:

  1. Xamarin Async Support Overview Xamarin异步支持概述
  2. Asynchronous Operations with Xamarin 使用Xamarin进行异步操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM