C#C
C#3y ago
Thalnos

✅ run async funtion from constructor

we can't use await keyword in the constructor because we can't add async keyword to it. How to run a function that returns a Task asynchronously then from the constructor? Or do we have to run it synchronously in that case?

I remember vaguely that there was an old legacy way in .Net to run Tasks asynchronously without the await keyword. I believe it was ContinueWith(), is that what we wanna do in that case?

If we do run it synchronously instead, what is the best way to run a Task synchronously out of the many possible existing ways? There is Task.Result, Task.Wait() Task.GetAwaiter().GetResult(), Task.RunSynchronously() and probably 20 more ways I don't know yet xD which one is generally prefered?
Was this page helpful?