-
Notifications
You must be signed in to change notification settings - Fork 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
Champion "Async Main" (C# 7.1) #97
Comments
Can we add a link from the above post to the proposal text? |
Done |
Meh. I don't really see much gain from this purely syntactic sugar change. The lack of an async Main may have been an oversight, but this solution doesn't resolve the oversight. Sure, now you can have an async Main, but you gain nothing over what you could have previously done with one line of code. An alternative async Main implementation could have passed in a cancellation token, and had the runtime host cancel the token if, say, a kill signal was received. |
I think this is still valuable, because it reduces confusion. Having to hit StackOverflow to learn that I need to add
This would be great. I'd love to see built-in passing of a cancellation token that was linked to the kill signal. 👍 |
I kind of agree. This is low hanging fruit, but also something very easily accomplished by a library. Where a library would have a significant advantage would be in the flexibility of handling different scenarios, such as wiring up a static class Program {
static int Main(string[] args) {
return Console.Async(async cancellationToken => {
await DoStuffAsync(cancellationToken);
return 0;
});
}
} |
There are a myriad of possible things you might want to do in your "entrypoint". You might want to respond to signals. You might want a serialized synchronization context. You might want ... etc. This feature is not meant to cover all possible scenarios, which @HaloFour highlights can be done by library. Rather, this simple feature is meant to eliminate the really annoying boilerplate that everyone has to find and copy or write to use Task-returning methods in their code: static void Main(string[] args) => MainAsync(args).GetAwaiter().GetResult();
static async Task Main(string[] args)
{
} becoming: static async Task Main(string[] args)
{
} such that a dev only needs to change |
Hi, I am trying new async Main in VS 2017 15.3 in one of my projects, but I get following exception:
Are there restrictions with using async Main? Thanks, |
Try adding |
Hi, Yes I changed language to 7.1 (it says there is no valid Main method if you don't). |
FYI @TyOverby For question above |
I didn't think of that. Edit: Actually, since Should that be automatically installed behind the scenes if you use the STAThread attribute? It would be super handy for |
@mblataric: Any attributes that you put on the Unfortunately this is a leaky abstraction that offers hints into how the feature was implemented. Under the hood, we create a new method called The decision to not move attributes was a tough one, but in the end we decided to for a few reasons
That leaves us where we are today: Attributes are not copied or moved to the hidden "real" Main method. @gafter: do you have any suggestions on where these details could be documented so that more people aren't left trying to interpret runtime errors? |
Could you special-case the STAThread attribute to be copied to $main and also automatically install a |
@jnm2: The compiler doesn't know what STAThread or SingleThreadedSynchronizationContext is. We try to keep the coupling between language and framework as minimal as possible. |
@TyOverby, I agree. However, it is likely worth providing a way to propagate attributes placed on the Otherwise, |
If Main is async, it's because you're going to await which will break the STAThread unless you install a synchronization context, which ideally would block and pump rather than starting a new STA thread. If it's going to block, a custom synchronization context API could still give the illusion that you await the pump and the continuation actually executes inside the pump, but that is weird and it's still boilerplate. May as well stick with synchronous Main for UI code if you aren't going to automatically install that sync context. |
I think we need to take this to the LDM. I suspect we will want to copy certain attributes such as this one. I will bring the issue up. |
Doesn't this issue is done and should be already closed? |
It hasn't yet been published in an updated C# language specification. |
See also dotnet/roslyn#1695 dotnet/roslyn#7476
The text was updated successfully, but these errors were encountered: