简体   繁体   中英

Async in Windows Forms

Is it possible to do async stuff in WinForms like we can in Metro apps on Windows 8?

For example:

protected async void Something()
{
    await FileIO.Something();
}

would be really cool to have in WinForms.

Yes.

Async is a C# 5.0 language feature and will work anywhere, as long as you have the supporting BCL types (.Net 4.5, or Microsoft.Bcl.Async from NuGet).

However, you should never write async void s anywhere, except for event handlers.

Yes, you can obtain an asynchronous UI using async-await in winform. You have to start an asynchronous task in event handler with Tast.Run method without awaiting it. To access any control property or method, you have to do it into main thread using Control.Invoke method.

At last, with this approach the ui remain ever active and can handle other event while previus code is running. then you have to write code to disable enable button or other event handling while code is running

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM