Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created August 29, 2012 19:49
Show Gist options
  • Select an option

  • Save chgeuer/3517883 to your computer and use it in GitHub Desktop.

Select an option

Save chgeuer/3517883 to your computer and use it in GitHub Desktop.
Async/await for WindowsRuntimeComponents
namespace WindowsRuntimeComponent1
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Foundation;
public interface IDoStuff
{
IAsyncAction DoItAsync();
IAsyncOperation<IList<string>> X();
}
// http://msdn.microsoft.com/en-us/library/windows/apps/br230301.aspx#AsyncOps
public sealed class Class1 : IDoStuff
{
IAsyncAction IDoStuff.DoItAsync()
{
return Task.Run(async () => { }).AsAsyncAction();
}
IAsyncOperation<IList<string>> IDoStuff.X()
{
return Task.Run<IList<string>>(async () =>
{
var data = await InternalStuff();
return data;
}).AsAsyncOperation<IList<string>>();
}
private async Task<IList<string>> InternalStuff()
{
return new System.Collections.Generic.List<string> { "Hallo" };
}
}
}
public async void clicked(object sender, RoutedEventArgs e)
{
IDoStuff c = new Class1();
await c.DoItAsync();
var list = await c.X();
foreach (var item in list)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment