I have a program where I use custom message to call some methods from WndProc
, like this:
protected override void WndProc(ref Message m)
{
switch ((uint)m.Msg)
{
case WM_QUIT:
Application.Exit();
return;
case WM_STARTOP:
Context.TændNas();
m.Result = new IntPtr(1);
break;
}
base.WndProc(ref m);
}
Now I want to make the methods asynchronous. However, I can't add the async
keyword to the WndProc
override.
If I make Context.TændNasAsync
an async
method, how can I call it from WndProc
?
Conclusion :
I go for @Hans Passant's suggestion, creating an async
eventhandler. That way I can call the async
method on the UI thread, while keeping it asynchron ( awaitable
).
This should work:
Task.Run(()=>Context.TaendNas());
or rather:
Task.Run(async ()=> await Context.TaendNas());
Use events :
event EventHandler onSomething;
protected override void WndProc(ref Message m)
{
switch ((uint)m.Msg)
{
case XXX:
onSomething(this, EventArgs.Empty);
break;
}
base.WndProc(ref m);
}
Somewhere in your code:
onSomething += async (s, e) => await Context.TændNas();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.