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 theTask.Result
you are blocking the main thread.Calling
.Result
in the UI thread will likely deadlock everything which is what you are experiencing. UseContinueWith
orasync void
withawait
.You can make your
Entry_TextChanged
async andawait
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 useDevice.BeginInvookeOnMainThread()
to run any code that needs to be run on UI thread.The better code would be :
The following links would help you understand the concepts better :