Async pattern

1,563 views
1,374 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,563
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Async pattern

  1. 1. New Asynchronous Pattern and Programming Jan 09, 2013 http://www.paxcel.net By- Amit Kumar Nigam Paxcel technologies. www.paxcel.netThis is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  2. 2. Topics Covered Introduction (Async & await) Synchronous Block Asynchronous Block (new way) Control flow in Async program Caller Info Attributes in C# 5.0 Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  3. 3. IntroductionThe Asynchronous Programming Model (APM), which has the formatBeginMethodName and EndMethodName. The Event based Asynchronous Pattern (EAP), which relies on assigning delegatesto event handlers that will be invoked when an event is triggered.The Task-based Asynchronous Pattern (TAP), which relies on the Task ParallelLibrary (TPL) Async and Await, you can use resources in the .NET Framework to create anasynchronous method as easily as you create a synchronous method.Asynchronous methods that you define by using async and await are referred to asasync methods. Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  4. 4. Synchronous Blockpublic int AccessTheWeb(){ // You need to add a reference to System.Net.Http to declare client. HttpClient client = new HttpClient(); // GetStringAsync returns a string. string getString = client.GetString("http://msdn.microsoft.com"); // You can do other work here DoIndependentWork(); // The return statement specifies an integer result. return getStringTask.Length;}void DoIndependentWork(){resultTextBox.Text += "working . . . . . /r/n";} Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  5. 5. Async Block (old way)const string Feed = "http://google.com";private void btnaSyncPrev_Click(object sender, RoutedEventArgs e){ StringBuilder builder = new StringBuilder(); this.AsynchronousCallServerTraditional(builder, 2);}public void AsynchronousCallServerTraditional(StringBuilder builder, int i){ if (i > 10) { MessageBox.Show( string.Format("Downloaded Successfully!!! Total Size : {0} chars.", builder.Length)); return; } this.tbStatus.Text = string.Format("Calling Server [{0}]..... ", i); WebClient client = new WebClient(); client.DownloadStringCompleted += (o,e) => { builder.Append(e.Result); this.AsynchronousCallServerTraditional(builder, i + 1); }; string currentCall = string.Format(Feed, i); client.DownloadStringAsync(new Uri(currentCall), null);} Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  6. 6. Async Block (new way)// Three things to note in the signature:// - The method has an async modifier.// - The return type is Task or Task<T>. (See "Return Types" section.)// Here, it is Task<int> because the return statement returns an integer.// - The method name ends in "Async."async Task<int> AccessTheWebAsync(){ // You need to add a reference to System.Net.Http to declare client. HttpClient client = new HttpClient(); // GetStringAsync returns a Task<string>. That means that when you await the // task youll get a string (urlContents). Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); // You can do work here that doesnt rely on the string from GetStringAsync. DoIndependentWork(); // The await operator suspends AccessTheWebAsync. // - AccessTheWebAsync cant continue until getStringTask is complete. // - Meanwhile, control returns to the caller of AccessTheWebAsync. // - Control resumes here when getStringTask is complete. // - The await operator then retrieves the string result from getStringTask. string urlContents = await getStringTask; // The return statement specifies an integer result. // Any methods that are awaiting AccessTheWebAsync retrieve the length value. return urlContents.Length;} Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  7. 7.  The method signature includes an Async or async modifier. The name of an async method, by convention, ends with an "Async" suffix. The return type is one of the following types: I. Task<TResult> if your method has a return statement in which the operand has type TResult. II. Task if your method has no return statement or has a return statement with no operand. III. Void (a Sub in Visual Basic) if youre writing an async event handler. The method usually includes at least one await expression, which marks a pointwhere the method cant continue until the awaited asynchronous operation iscomplete. In the meantime, the method is suspended, and control returns to themethods caller. Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  8. 8. Async Block (New way)// Three things to note in the signature:// - The method has an async modifier.// - The return type is Task or Task<T>. (See "Return Types" section.)// Here, it is Task<int> because the return statement returns an integer.// - The method name ends in "Async."async Task<int> AccessTheWebAsync(){ // You need to add a reference to System.Net.Http to declare client. HttpClient client = new HttpClient(); // GetStringAsync returns a Task<string>. That means that when you await the // task youll get a string (urlContents). Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); // You can do work here that doesnt rely on the string from GetStringAsync. DoIndependentWork(); // The await operator suspends AccessTheWebAsync. // - AccessTheWebAsync cant continue until getStringTask is complete. // - Meanwhile, control returns to the caller of AccessTheWebAsync. // - Control resumes here when getStringTask is complete. // - The await operator then retrieves the string result from getStringTask. string urlContents = await getStringTask; // The return statement specifies an integer result. // Any methods that are awaiting AccessTheWebAsync retrieve the length value. return urlContents.Length;} Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  9. 9. Control flow in Async program Paxcel technologies. www.paxcel.netThis is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.
  10. 10. class Test{static void SayMyName([CallerMemberName] string functionName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0){ Console.WriteLine("{0}:{1}({2})", filePath, functionName, lineNumber);}static void Main(string[ ] args){ SayMyName();}}Output:c:UsersAmitDocumentsVisual Studio11Projectstest_consolecstestTest.cs:Main(15) Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given to third parties without their consent.

×