c# - 瞬时代码延迟?任务使用是否正确?
问题描述
所以我完成了一些编码,但我不明白发生的一些结果。我将尝试在这里编写一些与我的真实代码非常相似的乱码。如果此信息很重要:它是一个 UWP 应用。
我将使用的一种命名textBlockTextMethod()
方法是带有一堆if
和if-else
语句的方法,这些语句总是最终会给出textBlock.Text
一个新文本(在这个例子中让它永远存在"End"
)。
所以首先让我们先看看我的代码看起来如何:
private void DoSomething()
{
textBlock.Text = "Start";
aIntVal = aMethod(); //aIntVal being a `int` variable
textBlockTextMethod();
}
private int aMethod()
{
int anotherIntVal = 0;
for(int i = 0; i < number; i++)
{
anotherIntVal += randomNum; // randomNum being a random 'int' value
}
return anotherIntVal
}
我对它有什么期望?我想要textBlock.Text
文本"Start"
,以便我可以Start
在 UWP 应用程序中直观地看到。然后aIntVal
应该int
从 接收一个值aMethod()
。aMethod()
大约需要 2-3 秒才能完成,之后textBlock.Text
应该有另一个值来自textBlockTextMethod
.
这里发生的事情是TextBlock
不会显示文本Start
。UWP 应用程序会冻结 2 秒,然后我会End
在已设置的 UWP 应用程序中看到textBlockTextMethod()
.
为什么我看不见Start
?我虽然它会在冻结期间可见。但是像这样感觉就像aMethod
被调用之前Start
设置为textBlock.Text
(或者更确切地说Start
从未设置为textBlock.Text
)
然后我尝试了一些不同的东西。这是我对代码的第二次尝试:
private async void DoSomething()
{
Task<string> aTask = taskMethod()
textBlock.Text = await aTask;
aIntVal = aMethod();
textBlockTextMethod();
}
private async Task<string> taskMethod()
{
textBlock.Text = "Start";
await Task.Delay(1);
return "";
}
private int aMethod()
{
int anotherIntVal = 0;
for(int i = 0; i < number; i++)
{
anotherIntVal += randomNum();
}
return anotherIntVal
}
这让我可以Start
在冻结期间在 UWP 应用中看到。但这不是很一致。大多数时候我都能看到Start
。但是有几次我遇到了与以前相同的问题,Start
但没有显示出来。
我注意到更改Task.Delay(1)
为Task.Delay(100)
会显示我的Start
一致性。为什么呢?
现在这是我现在使用的代码的第三次尝试:
private async void DoSomething()
{
textBlock.Text = "Start";
aIntVal = await aMethod();
textBlockTextMethod();
}
private async Task<int> aMethod()
{
int anotherIntVal = 0;
await Task.Run(() =>
{
for(int i = 0; i < number; i++)
{
anotherIntVal += randomNum();
}
});
return anotherIntVal
}
我的问题是:我的解决方案使用正确吗?有用。但这是因为我在这里很幸运,这仅在某些情况下有效,还是这是继续如何使用的常用方法Task
,async
并且await
?
解决方案
如果您想为受CPU 限制的工作负载释放UI,您可以执行以下操作
// async task
private async void ButtonClick()
{
// update the text box
textBlock.Text = "Start";
// if we have to wrap some CPU work, dont do it in the implementation
// use task run to call it.
// So... lets do something on the thread pool
// await the results, giving back the ui thread
var aIntVal = await Task.Run(() => aMethod());
// continue back on the UI thread and update the text box again
textBlock.Text = aIntVal;
}
// Vanilla method to do stuff
private int aMethod()
{
for (int i = 0; i < number; i++)
// awesome calculations here
return anotherIntVal;
}
其他资源
阅读斯蒂芬·克利里的一切
推荐阅读
- postgresql-9.6 - Postgres COPY FROM 是否应该更新 BRIN 索引?
- excel - 连接时如何将变体暗淡传递给字符串?
- ios - Realm Swift:对象已被删除或失效
- python - 如何添加 foriegnkey 对象并通过 Django rest Framework 中的单个端点将其连接为 post 对象
- c# - 混合使用 DateTime 和 DBNull 值时的 OrderBy 会引发错误
- r - 无法正确分组和求和
- javascript - 正则表达式:无效的组说明符名称 - 反应原生
- docker - 使用相同的 Docker 镜像文件权限因机器而异
- python - 如何将 dict 打印为正确的格式?
- javascript - 打字稿:按接口或类过滤对象