异步方法正确吗? ReSharper的警告 [英] async Methods correct? Resharper warning

查看:102
本文介绍了异步方法正确吗? ReSharper的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的方法 RecalcChartAsync 我做了一段时间密集的东西..所以我想我会做一些事情异步。

我要开始这两种方法 CreateHistogramAsync CalculatePropValues​​Async ,并在同时做一些东西在我的 RecalcChartsAsync 终于等到它完成。

 专用异步无效RecalcChartsAsync()
{
    变种直方图= CreateHistogramAsync();
    变种propValues​​ = CalculatePropValues​​Async();    //做一些其他的东西    等待直方图;
    等待propValues​​;
}私人异步任务CreateHistogramAsync()
{
    //做一些东西
}私人异步任务CalculatePropValues​​Async()
{
    //做一些东西
}

林不知道如果我做了正确的方式,因为ReSharper的给我在在CreateHistogramAsync和CalculatePropValueAsync async关键字以下警告:


  

这异步方法缺乏'等待'经营者和将同步运行。请考虑使用等待操作者等待非阻塞-API调用,...


现在我不知道如果我在正确的方式使用此异步的事情。


解决方案

  

现在我不知道如果我在正确的方式使用此异步的事情。


这听起来并不喜欢它。仅仅因为你有一个异步方法并不意味着它要在一个单独的线程中运行 - 这听起来像是那是你期待什么。当执行异步方法,它将同步运行 - 即就像正常的 - 直到遇到第一个的await 前pression。如果不这样做的有无的任何等待前pressions,这意味着它只是运行正常,唯一的区别是的方式,它包裹在一个状态机,以完成状态(异常等)再$ p $由任务psented。

我怀疑你应该改变你的 CreateHistogramAsync CalculatePropValues​​Async 方法只是同步:

 私人无效CreateHistogram()
{
    ...
}私人无效CalculatePropValues​​()
{
    ...
}

和使用<一个href=\"http://msdn.microsoft.com/en-gb/library/system.threading.tasks.task.run.aspx\"><$c$c>Task.Run在并行执行它们:

 专用异步无效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 my RecalcChartsAsync 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 an async method, it will run synchronously - i.e. just like normal - until it hits the first await expression. If you don't have any await 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 and CalculatePropValuesAsync 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屋!

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