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

编程入门 行业动态 更新时间:2024-10-24 21:25:05
本文介绍了从非异步方法调用异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我执行此操作的代码,但是一旦它开始调用异步函数,似乎该应用程序将无法响应,因此我无法做其他事情.我想将其运行到后台.

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.

我正在搜索,每3个字母,如果有匹配项,它将调用api以获取数据.输入3个字母后,它将调用API,并且由于应用程序没有响应,我无法输入更多字母.

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

预先感谢您的帮助.

推荐答案

您不需要GettingModel(string vinText)方法. 通过调用Task.Result,您正在阻塞主线程.

You don't need the GettingModel(string vinText) method. By calling the Task.Result you are blocking the main thread.

在UI线程中调用.Result可能会死锁所遇到的一切.将ContinueWith或async void与await一起使用.

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

您可以使您的Entry_TextChanged异步和await成为Web请求,以便它不会阻止UI.

You can make your Entry_TextChanged async and await the web request so that it doesn't block the UI.

如果您不需要让make用户等待操作完成,甚至可以在单独的线程上运行它并使用ContinueWith().如果要采用这种方式,请确保使用Device.BeginInvookeOnMainThread()运行需要在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.

更好的代码是:

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 :

  • Xamarin异步支持概述
  • 与Xamarin进行异步操作
  • Xamarin Async Support Overview
  • Asynchronous Operations with Xamarin
  • 更多推荐

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

    本文发布于:2023-11-14 13:35:02,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1587354.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:方法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!