I need to call a async
method from my Form1
constructor. Since a constructor can't have a return type, I can't add a async void
. I read that static constructor can be async
but I need to call methods from constructor that aren't static
, such as InitializeComponent()
(since it's the Form's constructor).
The class is:
public partial class Form1 : Form
{
InitializeComponent();
//some stuff
await myMethod();
}
I read this one too but I still don't know how to implement this (in my case) since the method still requires to use async
.
A simple answer for that: No, we can't! Currently, class constructors do not return types, and an asynchronous method should return a Task type. Don't worry about that, because there is a solution for this.
async function run() { let topic; debug("new TopicsModel"); try { topic = new TopicsModel(); } catch (err) { debug("err", err); } await topic. setup(); constructor. typescript.
Don't do this in the constructor but in the loaded event of the window instead. You can mark the loaded eventhandler as async.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With