Asynchronous programming

23,757 views

Published on

Slides from my LIDNUG presentation on Asynchronous Programming.

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

No Downloads
Views
Total views
23,757
On SlideShare
0
From Embeds
0
Number of Embeds
20,533
Actions
Shares
0
Downloads
73
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Asynchronous programming

  1. 1. Author. Blogger. Speaker. C# MVP. Geek.
  2. 2. 12:38
  3. 3. 12:38 Load app content Sort data Process records Asynchronous task
  4. 4. UsingTasks
  5. 5. UsingAsyncandAwait
  6. 6. private async Task RunAsync() { var x = 10; await Task.Delay(2000); Debug.WriteLine(x); }
  7. 7. private Task RunAsync() { <RunAsync>d__1 stateMachine = new <RunAsync>d__1 { <>t__builder = AsyncTaskMethodBuilder.Create(), <>1__state = -1 }; stateMachine.<>t__builder.Start<<RunAsync>d__1>(ref stateMachine); return stateMachine.<>t__builder.Task; }
  8. 8. if (num != 0) { this.<x>5__1 = 10; awaiter = Task.Delay(2000).GetAwaiter(); if (!awaiter.IsCompleted) { . . . . . . . . . . . . return; } } else { . . . . . } awaiter.GetResult(); awaiter = new TaskAwaiter(); Debug.WriteLine(this.<x>5__1);
  9. 9. private async void RunAsync() { var x = 10; await Task.Delay(2000); Debug.WriteLine(x); }
  10. 10. private void RunAsync() { <RunAsync>d__1 stateMachine = new <RunAsync>d__1 { <>t__builder = AsyncVoidMethodBuilder.Create(), <>1__state = -1 }; stateMachine.<>t__builder.Start<<RunAsync>d__1>(ref stateMachine); }
  11. 11. CloserlookatAsyncandAwait
  12. 12. AsyncandAwaitinASP.NET
  13. 13. $.post("ajax/test.html", function (data) { $(".result").html(data); })
  14. 14. Task.Run(() => { /* Lots of work to do! */ }); Task<string>.Run(() => { /* Lots of work to do! */ return "Hello World!"; });
  15. 15. Task.Run(() => { /* Lots of work to do! */ }) .ContinueWith((t) => { Debug.WriteLine("Done!"); }); await Task.Run(() => { /* Lots of work to do! */ }); Debug.WriteLine("Done!");
  16. 16. Task.Run(() => "Hello World!") .ContinueWith((task) => Debug.WriteLine(task.Result)); var result = await Task.Run(() => "Hello World!"); Debug.WriteLine(result);
  17. 17. private Task RunAsync() { <RunAsync>d__1 stateMachine = new <RunAsync>d__1 { <>t__builder = AsyncTaskMethodBuilder.Create(), <>1__state = -1 }; stateMachine.<>t__builder.Start<<RunAsync>d__1>(ref stateMachine); return stateMachine.<>t__builder.Task; }
  18. 18. Task.Run(() => { /* Lots of work to do! */ }) .ContinueWith((t) => { Debug.WriteLine("Done!"); }); await Task.Run(() => { /* Lots of work to do! */ }); Debug.WriteLine("Done!");
  19. 19. if (num != 0) { this.<x>5__1 = 10; awaiter = Task.Delay(2000).GetAwaiter(); if (!awaiter.IsCompleted) { . . . . . . . . . . . . return; } } else { . . . . . } awaiter.GetResult(); awaiter = new TaskAwaiter(); Debug.WriteLine(this.<x>5__1);
  20. 20. Task.Delay(1).ContinueWith((t) => { Dispatcher.Invoke(() => { }); }).Wait(); RunAsync().Wait();
  21. 21. Author. Blogger. Speaker. C# MVP. Geek.

×