Created
August 29, 2012 19:49
-
-
Save chgeuer/3517883 to your computer and use it in GitHub Desktop.
Async/await for WindowsRuntimeComponents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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