异步方法正确吗? ReSharper的警告 [英] async Methods correct? Resharper warning
问题描述
在我的方法 RecalcChartAsync
我做了一段时间密集的东西..所以我想我会做一些事情异步。
我要开始这两种方法 CreateHistogramAsync
CalculatePropValuesAsync
,并在同时做一些东西在我的 RecalcChartsAsync
终于等到它完成。
专用异步无效RecalcChartsAsync()
{
变种直方图= CreateHistogramAsync();
变种propValues = CalculatePropValuesAsync(); //做一些其他的东西 等待直方图;
等待propValues;
}私人异步任务CreateHistogramAsync()
{
//做一些东西
}私人异步任务CalculatePropValuesAsync()
{
//做一些东西
}
林不知道如果我做了正确的方式,因为ReSharper的给我在在CreateHistogramAsync和CalculatePropValueAsync async关键字以下警告:
这异步方法缺乏'等待'经营者和将同步运行。请考虑使用等待操作者等待非阻塞-API调用,...
块引用>现在我不知道如果我在正确的方式使用此异步的事情。
解决方案
现在我不知道如果我在正确的方式使用此异步的事情。
块引用>这听起来并不喜欢它。仅仅因为你有一个
异步
方法并不意味着它要在一个单独的线程中运行 - 这听起来像是那是你期待什么。当执行异步
方法,它将同步运行 - 即就像正常的 - 直到遇到第一个的await
前pression。如果不这样做的有无的任何等待
前pressions,这意味着它只是运行正常,唯一的区别是的方式,它包裹在一个状态机,以完成状态(异常等)再$ p $由任务psented。我怀疑你应该改变你的
CreateHistogramAsync
和CalculatePropValuesAsync
方法只是同步:私人无效CreateHistogram()
{
...
}私人无效CalculatePropValues()
{
...
}和使用<一个href=\"http://msdn.microsoft.com/en-gb/library/system.threading.tasks.task.run.aspx\"><$c$c>Task.Run$c$c>在并行执行它们:
专用异步无效RecalcChartsAsync()
{
VAR直方图= Task.Run((动作)CreateHistogram);
VAR propValues = Task.Run((动作)CalculatePropValues); //做一些其他的东西 等待直方图;
等待propValues;
}in my Method
RecalcChartAsync
i do some time intensive stuff.. so i thought i'll do some things async.I want to start the two Methods
CreateHistogramAsync
CalculatePropValuesAsync
and in the meanwhile do some stuff in myRecalcChartsAsync
and finally wait for it to complete.private async void RecalcChartsAsync() { var histogram = CreateHistogramAsync(); var propValues = CalculatePropValuesAsync(); //do some other stuff await histogram; await propValues; } private async Task CreateHistogramAsync() { //do some stuff } private async Task CalculatePropValuesAsync() { //do some stuff }
Im not sure if i am doing it the right way because ReSharper gives me the following warning at the async Keyword at CreateHistogramAsync and CalculatePropValueAsync:
This async method lacks 'await' operators and will run synchronously. Consider using the await operator to await non-blocking API calls, ...
Now i am not sure if i am using this async thing in the correct way.
解决方案Now i am not sure if i am using this async thing in the correct way.
It doesn't sound like it. Just because you have an
async
method doesn't mean it's going to run on a separate thread - and it sounds like that's what you're expecting. When you execute anasync
method, it will run synchronously - i.e. just like normal - until it hits the firstawait
expression. If you don't have anyawait
expressions, that means it will just run as normal, the only difference being the way that it's wrapped up in a state machine, with the completion status (exceptions etc) represented by a task.I suspect you should change your
CreateHistogramAsync
andCalculatePropValuesAsync
methods to be just synchronous:private void CreateHistogram() { ... } private void CalculatePropValues() { ... }
and use
Task.Run
to execute them in parallel:private async void RecalcChartsAsync() { var histogram = Task.Run((Action) CreateHistogram); var propValues = Task.Run((Action) CalculatePropValues); //do some other stuff await histogram; await propValues; }
这篇关于异步方法正确吗? ReSharper的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!