Using 'async' in a console application in C#
In most project types, your async
"up" and "down" will end at an async void
event handler or returning a Task
to your framework.
However, Console apps do not support this.
You can either just do a Wait
on the returned task:
static void Main()
{
MainAsync().Wait();
// or, if you want to avoid exceptions being wrapped into AggregateException:
// MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
...
}
or you can use your own context like the one I wrote:
static void Main()
{
AsyncContext.Run(() => MainAsync());
}
static async Task MainAsync()
{
...
}
More information for async
Console apps is on my blog.
how to create async/await console app c# clock
By calling clock()
from Main
you are starting an asynchronous task without waiting for it to complete.
You can make clock()
return a Task
and use clock().Wait()
block Main
method until the task is completed (which will never be, because clock()
contains an infinite loop and will never end).
Without Wait()
ing for the task to complete, the Main
will run to completion, causing the application to close.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main 1");
DoSomethingAsync().Wait();
Console.WriteLine("Main 2");
}
public static async Task DoSomethingAsync()
{
Console.WriteLine("DoSomethingAsync 1");
await Task.Delay(1000);
Console.WriteLine("DoSomethingAsync 2");
}
}
Output of the application:
Main 1
DoSomethingAsync 1
(delay of 1 second)
DoSomethingAsync 2
Main 2
Without Wait()
it would have been:
Main 1
DoSomethingAsync 1
Main 2
or more likely it could have been:
Main 1
Main 2
DoSomethingAsync 1
C#: call async method inside Main of console application leads to compilation failure
Your Main
method doesn't comply with one of the valid signatures listed here.
You probably want to use this:
public static async Task Main(string[] args)
A Task
needs to be returned, so that the runtime knows when the method is complete; this couldn't be determined with void
.
The compiler achieves this by generating a synthetic entry point:
private static void $GeneratedMain(string[] args) =>
Main(args).GetAwaiter().GetResult();
async and await in console app
await Foo();
means that the rest of the method won't run until after the Task
returned by Foo
has completed. That's what it means to await
something; the method won't continue until that Task
has completed. As such, Console.WriteLine("----- Exit main");
won't run until after Foo
has completed, which won't be until after it has already written out the time it took.
Related Topics
Notify Observablecollection When Item Changes
Converting a .Net Func<T> to a .Net Expression<Func<T>>
Writing to a Textbox from Another Thread
Calculating the Difference in Months Between Two Dates
How to Set a Program to Launch at Startup
What Is the Use of "Ref" for Reference-Type Variables in C#
Programmatically Determine a Duration of a Locked Workstation
How to Run an Exe File from My C# Code
C# Convert String from Utf-8 to Iso-8859-1 (Latin1) H
Deserialize Collection of Interface-Instances
How to Check the Number of Bytes Consumed by a Structure
Write a Well Designed Async/Non-Async API
How to Remove All Non Alphanumeric Characters from a String Except Dash