1. New Asynchronous Pattern
and
Programming
Jan 09, 2013
http://www.paxcel.net
By- Amit Kumar Nigam
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.
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. 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.
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. 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";
}
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. 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. 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;
}
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. 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.
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. 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;
}
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. Control flow in Async program
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.
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 Studio
11Projectstest_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.