-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AsyncAwaitMayBeElidedHighlighting on async void shouldn't appear #28
Comments
So there's nothing wrong with the idea of eliding async/await in public async void Foo()
{
await BarAsync();
}
public Task BarAsync()
{
return Task.CompletedTask;
} Today, if you choose to elide async/await inside public void Foo()
{
return BarAsync();
} Which obviously is a compiler error. When the return type of the method is public void Foo()
{
BarAsync();
} There is no problem in doing it this way for most applications. I just tested it with a WPF app and the Dispatcher still caught an exception fine. It may cause issues with exception handling in others. Edit: clarified language |
I agree with Paul that - syntactically - his recommendation would work. However, I believe this shouldn't be suggested at all for
would now become
which does not, in fact, wait for 5 seconds before exiting the method. |
@mdpopescu Good point. Although given that async/await should only be properly elided for the last call in the method, there could not be any code after it anyways (or it would not be elided), so I believe that would not matter. It certainly wouldn't matter much to the caller, because as soon as it gets to |
I don't understand what you mean, @paulirwin -- I gave an example where the change you're suggesting (eliding the async/await) would cause issues in an async void method. I actually had that happen in a program - the add-on suggested changing it, I removed the return manually and then realized that the Task.Delay would now be ineffective, changing the meaning of the code. |
@mdpopescu With async/await: class Program
{
static async Task Main(string[] args)
{
Foo();
Console.WriteLine("Done calling foo");
await Task.Delay(TimeSpan.FromSeconds(10));
}
public static async void Foo()
{
await Task.Delay(TimeSpan.FromSeconds(5));
}
} When you run this, The (properly) elided version: class Program
{
static async Task Main(string[] args)
{
Foo();
Console.WriteLine("Done calling foo");
await Task.Delay(TimeSpan.FromSeconds(10));
}
public static void Foo()
{
Task.Delay(TimeSpan.FromSeconds(5));
}
} Just like with the Say you were to have code after the delay, in which case it would matter: class Program
{
static async Task Main(string[] args)
{
Foo();
Console.WriteLine("Done calling foo");
await Task.Delay(TimeSpan.FromSeconds(10));
}
public static async void Foo()
{
await Task.Delay(TimeSpan.FromSeconds(5));
Console.WriteLine("Done delaying");
}
} In this case, However, this version cannot not be elided, because there is code after the The core bug here IMO is that choosing the eliding action adds a |
@mdpopescu After posting my last comment, I did think of one case where it makes a difference: debugging. In the elided version, if you step over the |
Ah... thank you. I did not realize that async void would return immediately (I had an event handler with that code and the only testing I did was with the debugger... which, as you point out, gave me a false idea). Thanks, I learned something new here! |
No description provided.
The text was updated successfully, but these errors were encountered: