本文介绍了查找code awaitable方法与Visual Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有其中异步方法被称为在code,而不 在前面的问题等待它。有没有一种方法来查找所有不具有的awaitable方法等待

I have a problem where async methods are being called in the code without await in front of it. Is there a way to find all the awaitable methods that do not have await?

修改 - 我特别关心的是在多个异步方法调用(忽略返回值)的情况下,但只有一个具有的await 这是足以让Visual Studio中不会发出警告了。

Edit - I'm particularly concerned with the scenario where multiple async methods are being called (ignoring the return values), but only one has await which is enough to make Visual Studio not warn about it.

推荐答案

如果您使用ReSharper的,把解决方案范围的分析,你的方法将要返回的不被期待已久的任务将变灰方法签名的任务部分出由于不使用返回值。这里需要说明的是,这只能找到没有被解决方案中的任何地方期待已久的方法;警告会离开后,一个或多个用途进行更新,以等待(或使用/引用工作)。

If you use ReSharper and turn solution-wide analysis on, your methods that are returning tasks that are not being awaited will have the Task portion of the method signature grayed out due to "return value is not used." The caveat here is that this will only find methods that are not being awaited anywhere in your solution; the warning will go away after one or more usages are updated to await (or use/reference the Task).

如果你正在寻找不包含的await调用(这意味着他们不需要被贴上异步),ReSharper的会告诉你太以类似的方式。异步方法

If you're looking for async methods that don't contain an await call (meaning they don't need to be labeled async), ReSharper will tell you about that too in a similar fashion.

    class AClass
    {
        public async void Foo() //async grayed out
        {
            DoSomethingAsync();
            Console.WriteLine("Done");
        }

        public Task<bool> DoSomethingAsync() //Task<bool> grayed out
        {
            return Task.Run(() => true);
        }
    }

请注意这是不行的,如果你有code,看起来像这样:

Note this will not work if you have code that looks like this:

    class AClass
    {
        public async void Foo()
        {
            bool b = DoSomethingAsync().Result;
            Console.WriteLine("Done");
        }

        public Task<bool> DoSomethingAsync()
        {
            return Task.Run(() => true);
        }
    }

异步关键字,如present,仍然会被标记,这意味着你也许可以找出pretty快速任务不被期待已久的,但如果调用方法没有标记异步你的运气了。

The async keyword, if present, will still be flagged, which means you can probably figure out pretty quickly a Task is not being awaited, but if the calling method is not marked async you are out of luck.

这篇关于查找code awaitable方法与Visual Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:18