[英]Trying to start a dialog after bool task, application stucks
当我在布尔按钮应用程序内部使用异步任务结果时卡住
async Task<bool> task1()
{
await Task.Run(() =>
{
for (int a = 0; a <= 1000000000; a++)
{
}
});
return true;
}
private void Start_Click(object sender, EventArgs e)
{
setDialog(true);
if (task1().Result==true)
setDialog(false);
}
private void setDialog(bool show)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetView(Resource.Layout.layout1);
Dialog dialog = builder.Create();
dialog.SetCancelable(false);
if (show) dialog.Show();
else dialog.Dismiss();
}
当我按下开始按钮时,我的应用程序被卡住,无法让我做任何事情。 如果我将在按钮事件中使用任务,那么一切都很好
您使应用程序陷入僵局。 这是: task1().Result
同步等待,使任务无法实现。 像这样使用async / await:
private async void Start_Click(object sender, EventArgs e)
{
setDialog(true);
var result = await task1();
if (result)
setDialog(false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.