-1

How to call on async Task function from Void class?

Let's say I got this built up like this,

I Want to call the methods in Run() in sync, but in the DoSomeStuffAsync and DoSomeStuffAsync 2 It requires await on some functions that has async awaitable functions.

My Question is, how can I in DoTask1 e.g. make the task to run sync? Like now the tasks just starts it and it doesn't wait until it finish at all.

    static void Main(string[] args)
    {
        Run();
    }

    public void Run()
    {
        DoTask1();
        DoTask2();
    }

    public void DoTask1()
    {
        var tasks = Task.Run(async () =>
        {
            await DoSomeStuffAsync();
            await DoSomeStuffAsync2();
        });

        Task.WaitAll(tasks);
    }

    public void DoTask2()
    {
        var tasks = Task.Run(async () =>
        {
            await DoSomeStuffAsync();
            await DoSomeStuffAsync2();
        });

        Task.WaitAll(tasks);
    }

    private async Task DoSomeStuffAsync(int daysBack)
    {
        try
        {                
            var data= await GetData();                
            var objj = GenerateObjects();

            await InsertToDb(objj ));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
     }

    private async Task DoSomeStuffAsync2(int daysBack)
    {
        try
        {                
            var data= await GetOtherData();                
            var objj = GenerateObjects();

            await InsertToDb(objj ));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
     }

2 Answers2

1

The Console app static void Main() is calling on the Run() function.

You have a couple of options. For both of the options below, you should first make your Run method properly asynchronous (i.e., async Task).

If this is a .NET Core project, then option A is to make Main an async Task method. Then you can use async all the way.

If not, then you can have a single blocking call. I recommend that this go into your Main method, as such:

static void Main()
{
  Run().GetAwaiter().GetResult();
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Hi, I did have it as async task in my main and run, but then I got told that it is wrong to have it as async Task, as it doesn't do async operations in main and run. So I tried to cut it down by removing all async Task where it run sync, and then do async task on the place where it should run async. –  Sep 17 '19 at 09:00
  • In order for code to run asynchronously, it must be called asynchronously. The proper way to use `async` is "async all the way". – Stephen Cleary Sep 17 '19 at 18:21
-2

Async void is a bad choice, generates compiler warnings, if an exception is uncaught there, your application is dead and you won’t probably have a proper call stack to debug with. But if you want, you already have the solution just call directly the async task method. And wrappe it in a exception catcher

static void Main(string[] args)
{
   Task callTask = Task.Run(() => Run());
   callTask.Wait();
}

public async Task Run()
{
    await DoSomeStuffAsync();
}
Pedro Brito
  • 263
  • 1
  • 9