Async pattern

10
Paxcel technologies. www.paxcel.net This is the exclusive property of Paxcel Technologies. This may not be reproduced or given http://www.paxcel.net New Asynchronous Pattern and Programming Jan 09, 2013 By- Amit Kumar Nigam

Transcript of Async pattern

Page 1: Async pattern

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.

http://www.paxcel.net

New Asynchronous Patternand

ProgrammingJan 09, 2013

By- Amit Kumar Nigam

Page 2: Async pattern

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.

Topics Covered Introduction (Async & await)

Synchronous Block

Asynchronous Block (new way)

Control flow in Async program

Caller Info Attributes in C# 5.0

Page 3: Async pattern

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.

Introduction The Asynchronous Programming Model (APM), which has the format

BeginMethodName and EndMethodName.

The Event based Asynchronous Pattern (EAP), which relies on assigning delegates to event handlers that will be invoked when an event is triggered.

The Task-based Asynchronous Pattern (TAP), which relies on the Task Parallel Library (TPL)

Async and Await, you can use resources in the .NET Framework to create an asynchronous method as easily as you create a synchronous method.Asynchronous methods that you define by using async and await are referred to as async methods.

Page 4: Async pattern

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.

Synchronous Block

public 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";

}

Page 5: Async pattern

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.

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);

}

Page 6: Async pattern

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.

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 you'll get a string (urlContents). Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

// You can do work here that doesn't rely on the string from GetStringAsync. DoIndependentWork();

// The await operator suspends AccessTheWebAsync. // - AccessTheWebAsync can't 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;}

Page 7: Async pattern

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.

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 you're writing an async event handler.

The method usually includes at least one await expression, which marks a point where the method can't continue until the awaited asynchronous operation is complete. In the meantime, the method is suspended, and control returns to the method's caller.

Page 8: Async pattern

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.

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 you'll get a string (urlContents). Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

// You can do work here that doesn't rely on the string from GetStringAsync. DoIndependentWork();

// The await operator suspends AccessTheWebAsync. // - AccessTheWebAsync can't 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;}

Page 9: Async pattern

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.

Control flow in Async program

Page 10: Async pattern

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.

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:\Users\Amit\Documents\Visual Studio 11\Projects\test_console\cstest\Test.cs:Main(15)