-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Proposal: async blocks #10503
Comments
@alrz You can mark I think that #7169 would be more apt to solve issues like these, however since you'd still need a task-like analog you still can't really make a synchronous method signatures asynchronous (while expecting to return the result to the caller.) Also, wouldn't local functions be sufficient in declaring nested "blocks" of asynchronous code that could could invoke from within a non- void IActionInvoker.Execute(MethodInfo method) {
var task = method.Invoke(null) as Task;
UpdateIndicator();
async void UpdateIndicator() {
ShowIndicator();
try { await task; }
catch(Exception ex) { ShowError(ex); }
finally { HideIndicator(); }
}
} |
You can use
This would apply to |
@alrz Making entry points asynchronous is a special case because it is practically the only case where blocking on an asynchronous call is considered safe. Elsewhere you are inviting deadlocks since you cannot know if asynchronous methods called depends on a synchronization context. Having language support for this would encourage a very bad practice. |
@alrz You describe the syntax you want to write, but not its semantics. What would it do? |
@gafter The idea is that it would get blocked if you |
In other words, an |
@gafter Same would apply to async Main, the point is to be able to use The difference of this and |
That sounds very unintuitive to me. In a |
Both of the described behaviors are very much considered bad practices in asynchronous code. The entry point is the one exception. As such I don't think that a general purpose mechanism should be introduced into the language as it would encourage people to use it outside of |
Fair enough. |
Currently, to use asynchronous features, you need to change the method signature to return a
Task
, but in some cases it would not be possible to do so, e.g. implementing an interface, writing event handlers or the entry point. In these cases developers often end up with highly discouragedasync void
,ContinueWith
calls, or manually created bridges which (to quote from #7476) "is a waste of time and can be a cause of bugs."It is proposed (#7476) to make this a special case for entry points but it's not the only place that prevents you to use
async
/await
and still there are chances that you will need to write these bridges or consider any other workarounds.It would be nice to be able to write an
async
block, which enables us to useawait
without changing the method signature and still don't lose readability of the code.This is not the exact translation but something similar should work for the
Main
method or in other contexts.The text was updated successfully, but these errors were encountered: