-2

Using a console project, I want to run the console window as UI to view and manage objects. At the same time, I want to run a simulation process that generates and manipulates the objects over time, that are managed in the console.

I thought i would make the simulation process asynchronous but I realised I can't really start it asynchronously since the console projects main method cannot be async. This won't work:

static async void Main(string[] args)
{}

So how is it supposed to be able to start a parallel thread using async?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Daarwin
  • 2,896
  • 7
  • 39
  • 69
  • 2
    `since the console projects main method cannot be async` - [yes it can](https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app/9212343#comment81152854_9208921). If you don't have access to C# 7.1, then see https://stackoverflow.com/q/9343594/11683. – GSerg Jul 15 '19 at 16:57

1 Answers1

1

You can use Wait if your async method returns a Task... here is an example from Benjarmin Perkins' blog:

static private async Task callWebApi()
{
  WebResponse response = await WebRequest
      .Create("http://**?**.azurewebsites.net/api/sleepy")
      .GetResponseAsync()
      .ConfigureAwait(false);;
  WriteLine(response.StatusCode.ToString());
}

public static void Main(string[] args)
{
  try
  {
    callWebApi().Wait();
  }
  catch (Exception ex)
  {
    WriteLine($"There was an exception: {ex.ToString()}");
  }
}
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108