简体   繁体   中英

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. 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.

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. By calling the Task.Result you are blocking the main thread.

Calling .Result in the UI thread will likely deadlock everything which is what you are experiencing. Use ContinueWith or async void with await .

You can make your Entry_TextChanged async and await the web request so that it doesn't block the 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. If you are going that route make sure you use Device.BeginInvookeOnMainThread() to run any code that needs to be run on UI thread.

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
  2. Asynchronous Operations with Xamarin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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