加载数据时异步和等待 [英] Async and Await When Loading Data

查看:50
本文介绍了加载数据时异步和等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

button1.KeyDown + = async(o,n)=>
{

//这是一个搜索功能,用户会输入

//文本框,然后在每个按键中,应用程序将连接

//到数据库并搜索是否找到了已键入的数据

//然后将其绑定到gridcontrol

var getData = Task.Run(()=> ; Call_To_Database_NotAsync);
var result = await getData;
gridcontrol.Datasource = result;
}

当用户快速键入并且与数据库的连接速度很慢时,网格控件是否可能根本不包含任何数据?


问候


 

解决方案

< blockquote>

我认为如果
Call_To_Database_NotAsync 或服务器端函数不支持多个并行执行,因此可以观察到不良影响。找到一种方法来取消过时的请求。


button1.KeyDown+=async(o,n)=> {

//This is a search function, the user will type something on

// a textbox then in every keypress, the application will connect

//to the database and search if the typed data has been found

//then bind it to a gridcontrol

var getData=Task.Run(()=> Call_To_Database_NotAsync); var result=await getData; gridcontrol.Datasource=result; }

Is there a possibility that the grid control will contain no data at all when the user types fast and the connection to the database is slow?

Regards

 

解决方案

I think that if Call_To_Database_NotAsync or server-side functions are not designed to support multiple parallel executions, then it is possible to observe undesirable effects. Find a way to cancel the requests that are obsolete.


这篇关于加载数据时异步和等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆