Visug async

870 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
870
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • +++ 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
  • Visug async

    1. 1. VisugAsync in Real Life Applications 1
    2. 2. http://proq.blogspot.com@GitteTitterhttp://www.qframe.be@qframedotnet 2
    3. 3. Overview• Async – The basics – Exceptions – Cancellation – JS – ...• In Real Life Applications• Recommendations 3
    4. 4. .NET: Where are we? C# 5.0 + VB 11.0 Windows Runtime + Async Async CTP VS11 Beta C# 4.0 + VB 10.0 Dynamic C# 3.0 + VB 9.0 Language Integrated Query C# 2.0 + VB 8.0 GenericsC# 1.0 + VB 7.0 Managed Code 4
    5. 5. Why Async?varvar 5
    6. 6. Why AsyncFast & Fluid 6
    7. 7. Can we already do this?Yes, we can! ... But it’s ugly. ... And hard to do. Begin... – End... Method pairs Callbacks 7
    8. 8. public IAsyncResult BeginCopyStreamToStream( Stream source, Stream destination, AsyncCallback callback, object state) Can we already do this? { var tcs = new TaskCompletionSource<object>(state); if (callback != null) tcs.Task.ContinueWith(_ => callback(tcs.Task)); var buffer = new byte[0x1000]; Action<IAsyncResult> readWriteLoop = null; readWriteLoop = iar => { try { for (bool isRead = iar == null; ; isRead = !isRead) {public void CopyStreamToStream(Stream source, Stream destination) switch (isRead) { case true:{ iar = source.BeginRead(buffer, 0, buffer.Length, readResult => { byte[] buffer = new byte[0x1000]; if (readResult.CompletedSynchronously) return; readWriteLoop(readResult); int numRead; }, null); if (!iar.CompletedSynchronously) return; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) break; { case false: int numRead = source.EndRead(iar); if (numRead == 0) { destination.Write(buffer, 0, numRead); tcs.TrySetResult(null); return; } } iar = destination.BeginWrite(buffer, 0, numRead,} writeResult => { if (writeResult.CompletedSynchronously) return; destination.EndWrite(writeResult); readWriteLoop(null); }, null); if (!iar.CompletedSynchronously) return; destination.EndWrite(iar); break; } } } catch (Exception e) { tcs.TrySetException(e); } }; readWriteLoop(null); return tcs.Task; } public void EndCopyStreamToStream(IAsyncResult asyncResult) { ((Task)asyncResult).Wait(); } 8
    9. 9. So, what’s new? C# and Visual Basic let you doasynchronous programming without callbacks 9
    10. 10. How do we do this?public void CopyStreamToStream(Stream source, Stream destination){ byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); }}public async Task CopyStreamToStreamAsync(Stream source, Stream destination){ byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) != 0) { await destination.WriteAsync(buffer, 0, numRead); }} 10
    11. 11. What is Async – The Basics 11
    12. 12. What is Async – The Basics 12
    13. 13. What is Async – The Basicspublic 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
    14. 14. DEMOSome basic async 14
    15. 15. So it actually gives me Task or Task<T> Yes. With different states. 15
    16. 16. What are those states?Created Start Started Completed Completed Close Closed Error Error 16
    17. 17. How do we handle errors?Just use try - catchtry { FileOpenPicker p = new FileOpenPicker(); p.FileTypeFilter.Add(".jpg"); MyButton.Content = (await p.PickSingleFileAsync()).FileName;} catch(Exception e) {} 17
    18. 18. DEMOException Handling 18
    19. 19. Are there still other states? Yes. There is one more. 19
    20. 20. What are those states? Cancelled CancelCreated Start Started Completed Completed Close Closed Error Error 20
    21. 21. And there’s also a way to communicate progress. 21
    22. 22. What are those states? Cancelled CancelCreated Start Started Completed Completed Close Closed Error Error 22
    23. 23. How do we do this?await FooAsync(…, cancel, progress); 23
    24. 24. DEMOCancellation & Progress 24
    25. 25. So, it’s always Task<T>? Erm, no, there’s also void(... And some other types, but we’ll get back to those) 25
    26. 26. DEMOThe problem with void 26
    27. 27. So what should I do with void then?async Task async voidFooAsync(…); Foo_Click(…); 27
    28. 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
    29. 29. 29
    30. 30. Except for those other return types that is...... But I still don’t want to tell you about those. First let’s ask another question. 30
    31. 31. Are there other languages I can use this in? Sure there are.VB C++ JavaScript 31
    32. 32. JavaScript Promisesvar start = new Date();Windows.Graphics.Imaging.BitmapDecoder.createAsync(stream).then( function (decoder) { var timeLabel = document.getElementById("TimeLabel"); timeLabel.innerText = new Date()- start + "ms"; }); 32
    33. 33. DEMOAsync in JavaScript 33
    34. 34. Was that a libraryI saw you using in that demo? Indeed you did. 34
    35. 35. I did have to alter some code, though...... And use another return type. Task IAsyncAction Task<T> IAsyncOperation<T> 35
    36. 36. DEMOAsync in a WinMD class library 36
    37. 37. 37
    38. 38. To sum up, we have somerecommendations for you. 38
    39. 39. TIP 1Don’t forget to cancel your awaits. 39
    40. 40. TIP 2 It’s easy to forgetthe ‘Async’ extension. 40
    41. 41. TIP 3 Awaiting a local variable inJavaScript gives you funky results. 41
    42. 42. TIP 4 Awaiting a void method reactsdifferent than awaiting a non-void method. 42
    43. 43. TIP 5Don’t use dialogs in your async methods. 43
    44. 44. TIP 6Don’t await in a catch clause. 44
    45. 45. TIP 7You end up with a whole lot of async 45
    46. 46. TIP 8 .Wait, WaitAll= synchronously waiting await, .ContinueWith= asynchronously waiting 46
    47. 47. 47
    48. 48. Let us know what you thinkhttp://proq.blogspot.com@GitteTitterhttp://www.qframe.be@qframedotnetinfo@QFrame.begitte.vermeiren@QFrame.be 48

    ×