Presentation is loading. Please wait.

Presentation is loading. Please wait.

IAsyncResult ar = BeginSomething(…); // Do other work, checking ar.IsCompleted int result = EndSomething(ar);

Similar presentations


Presentation on theme: "IAsyncResult ar = BeginSomething(…); // Do other work, checking ar.IsCompleted int result = EndSomething(ar);"— Presentation transcript:

1

2

3

4

5

6 IAsyncResult ar = BeginSomething(…); // Do other work, checking ar.IsCompleted int result = EndSomething(ar);

7 SomethingCompleted += (sender, e) => { // Do something with result }; SomethingAsync(…);

8 Task task = SomethingAsync(…); // Do other work, checking task.Status int result = task.Result;

9 int result = await SomethingAsync(…);

10

11

12

13

14 Busy Request Queue Thread Pool

15 public int SumPageSizes(IList uris) { int total = 0; foreach (var uri in uris) { var data = new WebClient().DownloadData(uri); total += data.Length; } return total; }

16 Request Queue Thread Pool

17 public int SumPageSizes(IList uris) { int total = 0; foreach (var uri in uris) { var data = new WebClient().DownloadData(uri); total += data.Length; } return total; }

18 public void SumPageSizesAsync(IList uris, Action callback) { SumPageSizesAsyncHelper(uris.GetEnumerator(), 0, callback); } private void SumPageSizesAsyncHelper(IEnumerator enumerator, int total, Action callback) { try { if (enumerator.MoveNext()) { var client = new WebClient(); client.DownloadDataCompleted += (sender, e) => { if (e.Error != null) { enumerator.Dispose(); callback(0, e.Error); } else SumPageSizesAsyncHelper(enumerator, total + e.Result.Length, callback); }; client.DownloadDataAsync(enumerator.Current); } else { enumerator.Dispose(); callback(total, null); } } catch (Exception ex) { enumerator.Dispose(); callback(0, ex); } }

19 public int SumPageSizes(IList uris) { int total = 0; foreach (var uri in uris) { var data = new WebClient().DownloadData(uri); total += data.Length; } return total; }

20 public async Task SumPageSizesAsync(IList uris) { int total = 0; foreach (var uri in uris) { var data = await new WebClient().DownloadDataTasksync(uri); total += data.Length; } return total; }

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43 Your code could be terminated at any time Use WebBackgrounder http://nuget.org/packages/WebBackgrounder/ Write a worker process for more complex tasks

44

45

46 Included for certain paid MSDN subscribers: Free Plan for up to 5 users Additional information at http://tfs.visualstudio.com http://tfs.visualstudio.com * Capability in preview – limits may apply. Authoring load tests requires Visual Studio Ultimate 2013 Preview. Version control (TFVC or Git) Comment on changesets & commits Work item tracking and tagging Team rooms Agile planning tools Feedback Management Agile Portfolio Management* Build* Web-based test case management* Load testing* New!

47 Activate and try out your Windows Azure MSDN benefit today & you could win an Aston Martin V8 Vantage! Learn more and activate today at http://aka.ms/AzureContesthttp://aka.ms/AzureContest

48 http://microsoft.com/msdn www.microsoft.com/learning http://channel9.msdn.com/Events/TechEd http://microsoft.com/technet

49

50

51


Download ppt "IAsyncResult ar = BeginSomething(…); // Do other work, checking ar.IsCompleted int result = EndSomething(ar);"

Similar presentations


Ads by Google