2

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.

1
  • 2
    Just change your event handler to an asynchronouse event handler, ie `async void Entry_TextChanged(){...;var result=await getModelForVIN()...;}' Commented Aug 10, 2016 at 8:20

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

The null check could also be replaced with String.IsNullOrEmpty. On the other hand, ModelVIN.Text will be null if not initialized. Why not just write ModelVIN.Text = result then?
@PanagiotisKanavos - Thanks for the catch, updated the code.
@PanagiotisKanavos - ModelVIN is something on a global scope and not a local variable, it might have a value before the execution of the function or not. The OP might want to preserve the previous value if it doesn't meet the condition.
Careful when using ContinueWith: stackoverflow.com/questions/18965200/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.