13. What is Async – The Basics
public async Task<XElement> GetXmlAsync(string url) {
var client = new HttpClient();
var response = await client.GetAsync(url);
var text = response.Content.ReadAsString();
return XElement.Parse(text);
}
public Task<XElement> GetXmlAsync(string url) {
var tcs = new TaskCompletionSource<XElement>();
var client = new HttpClient();
client.GetAsync(url).ContinueWith(task => {
var response = task.Result;
var text = response.Content.ReadAsString();
tcs.SetResult(XElement.Parse(text));
});
return tcs.Task;
}
13
15. So it actually gives me Task or Task<T>
Yes. With different states.
15
16. What are those states?
Created Start Started Completed Completed Close Closed
Error
Error
16
17. How do we handle errors?
Just use try - catch
try {
FileOpenPicker p = new FileOpenPicker();
p.FileTypeFilter.Add(".jpg");
MyButton.Content =
(await p.PickSingleFileAsync()).FileName;
} catch(Exception e) {}
17
27. So what should I do with void then?
async Task async void
FooAsync(…); Foo_Click(…);
27
28. What about those Task methods?
• Yielding control
await Task.Delay(5000);
await Task.Yield();
• Background running
var result = await Task.Run(() => { … work … });
• Parallel composition
Task first = await Task.WhenAny(task1, task2);
await Task.WhenAll(task1, task2);
28
48. Let us know what you think
http://proq.blogspot.com
@GitteTitter
http://www.qframe.be
@qframedotnet
info@QFrame.be
gitte.vermeiren@QFrame.be
48
Notas do Editor
+++ diene callback wordt uitgevoerd op de thread waarhij begonnen is => NO MORE DISPATCHER.INVOKE !!!!!
Show OnNavigatedTo of PublishHikeAndWaitPoint out:Async and await keywordsTask return typeAsync on a lambdaDrivers are being shown on the map, in the mean time UI stays responsiveHikers are not yet being shown = rest of the method is a callbackI can already click on a driver + second popup = rest of the method is really a callback.
STOP IIS SERVICE !!!In RestCaller en HikerRoute.xaml.csPoint out that:It’s just try-catch