I’ve been using C# for quite a while, but somehow have not yet had the pleasure of using the whole await/async thing new in .Net 4. Well, now I’m making a console application that links to the WinRT libraries and WinRT is really.. well, asynchronous.
One thing I’m needing to do right now is install a package. So, I use PackageManager.AddPackageAsync This is of course asynchonous and returns an IAsyncOperationWithProgress
I know I could do something like
bool Done=false;
void Foo()
{
var tmp=new PackageManager.AddPackageAsync(...);
tmp.Completed= ... FooBar;
while(!Done){}
}
void FooBar(...)
{
Done=true;
}
But I’m pretty sure the whole async and await thing was designed for exactly this scenario. However, I don’t understand how to use it in this context on an IAsyncOperationWithProgress
.Net includes a
GetAwaiter()extension method that makesIAsyncOperationWithProgressawaitable.You can just write
await tmp;in anasyncmethod.Your
asyncmethod will return aTask(orTask<T>), which you canawaitfrom another async method, or callWait()to synchronously block.