warning this call is not awaited, execution of the current method continues Ask Question

warning this call is not awaited, execution of the current method continues Ask Question

Just got VS2012 and trying to get a handle on async.

Let's say I've got an method that fetches some value from a blocking source. I don't want caller of the method to block. I could write the method to take a callback which is invoked when the value arrives, but since I'm using C# 5, I decide to make the method async so callers don't have to deal with callbacks:

// contrived example (edited in response to Servy's comment)
public static Task<string> PromptForStringAsync(string prompt)
{
    return Task.Factory.StartNew(() => {
        Console.Write(prompt);
        return Console.ReadLine();
    });
}

Here's an example method that calls it. If PromptForStringAsync wasn't async, this method would require nesting a callback within a callback. With async, I get to write my method in this very natural way:

public static async Task GetNameAsync()
{
    string firstname = await PromptForStringAsync("Enter your first name: ");
    Console.WriteLine("Welcome {0}.", firstname);

    string lastname = await PromptForStringAsync("Enter your last name: ");
    Console.WriteLine("Name saved as '{0} {1}'.", firstname, lastname);
}

So far so good. The problem is when I call GetNameAsync:

public static void DoStuff()
{
    GetNameAsync();
    MainWorkOfApplicationIDontWantBlocked();
}

The whole point of GetNameAsync is that it's asynchronous. I don't want it to block, because I want to get back to the MainWorkOfApplicationIDontWantBlocked ASAP and let GetNameAsync do its thing in the background. However, calling it this way gives me a compiler warning on the GetNameAsync line:

Warning 1   Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

I'm perfectly aware that "execution of the current method continues before the call is completed". That's the point of asynchronous code, right?

I prefer my code to compile without warnings, but there's nothing to "fix" here because the code is doing exactly what I intend it to do. I can get rid of the warning by storing the return value of GetNameAsync:

public static void DoStuff()
{
    var result = GetNameAsync(); // supress warning
    MainWorkOfApplicationIDontWantBlocked();
}

But now I have superfluous code. Visual Studio seems to understand that I was forced to write this unnecessary code, because it suppresses the normal "value never used" warning.

I can also get rid of the warning by wrapping GetNameAsync in a method that's not async:

    public static Task GetNameWrapper()
    {
        return GetNameAsync();
    }

But that's even more superfluous code. So I have to write code I don't need or tolerate an unnecessary warning.

Is there something about my use of async that's wrong here?

ベストアンサー1

I'm quite late to this discussion, but there is also the option to use the #pragma pre-processor directive. I have some async code here and there that I explicitly do not want to await in some conditions, and I dislike warnings and unused variables just like the rest of you:

#pragma warning disable 4014
SomeMethodAsync();
#pragma warning restore 4014

The "4014" comes from this MSDN page: Compiler Warning (level 1) CS4014.

See also the warning/answer by @ryan-horath here https://stackoverflow.com/a/12145047/928483.

待機されていない非同期呼び出し中にスローされた例外は失われます。この警告を回避するには、非同期呼び出しの Task 戻り値を変数に割り当てる必要があります。これにより、戻り値で示される、スローされたすべての例外にアクセスできるようになります。

C# 7.0 のアップデート

C# 7.0 では、変数を破棄するという新しい機能が追加されました。破棄 - C# ガイド、これもこの点で役立ちます。

_ = SomeMethodAsync();

おすすめ記事