Download presentation
Presentation is loading. Please wait.
Published byMaud Armstrong Modified over 9 years ago
1
Cole Durdan
2
What is asynchronous programming? Previous patterns Task Based Async Programming .NET 4.5 Keywords What happens in an async. method? Demo
3
“Asynchronous” means API does not block calling thread Multithreading? ◦ Not Necessarily Doesn’t lock UI on large computations Synchronous: |----A-----| |-----B-----------| |-------C------| Asynchronous: |----A-----| |-----B-----------| |-------C------|
4
Web Access Database Requests Working with files Working with images WCF programming Working with sockets With UI that need to be responsive
5
The Asynchronous Programming Model (APM) ◦ BeginMethodName and EndMethodName ◦ IAsyncResult ◦ Keep track of states The Event based Asynchronous Pattern (EAP) ◦ Assigns delegates to event handlers that are invoked when an event is triggered ◦ introduced in the.NET Framework version 2.0
6
public static IAsyncResult BeginCopyTo(Stream source, Stream destination) { var tcs = new TaskCompletionSource(); byte[] buffer = new byte[0x1000]; Action readWriteLoop = null; readWriteLoop = iar => { try { for (bool isRead = iar == null; ; isRead = !isRead) { switch (isRead) { case true: iar = source.BeginRead(buffer, 0, buffer.Length, readResult => { if (readResult.CompletedSynchronously) return; readWriteLoop(readResult); }, null); if (!iar.CompletedSynchronously) return; break; case false: int numRead = source.EndRead(iar); if (numRead == 0) { tcs.TrySetResult(true); return; } iar = destination.BeginWrite(buffer, 0, numRead, writeResult => { try { if (writeResult.CompletedSynchronously) return; destination.EndWrite(writeResult); readWriteLoop(null); } catch(Exception e) { tcs.TrySetException(e); } }, null); if (!iar.CompletedSynchronously) return; destination.EndWrite(iar); break; } } } } catch(Exception e) { tcs.TrySetException(e); } }; readWriteLoop(null); return tcs.Task; } public static void EndCopyTo(IAsyncResult asyncResult) { ((Task)asyncResult).Wait(); }
7
public static async void CopyToAsync(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) > 0) { await destination.WriteAsync(buffer, 0, numRead); } }
8
A lot of extra code Difficult to… ◦ Read ◦ Write ◦ Maintain ◦ Debug
9
Relies on the Task Parallel Library (TPL) System.Threading System.Threading.Task namespace ◦ Introduced in.NET 4.0 Framework ◦ New support in 4.5 TPL is the preferred way to write multithreaded and parallel code
10
Keywords ◦ “Async” ◦ “Await” Return Type of Object Task ◦ Task if sub or void ◦ Task Naming Convention ◦ MethodNameAsync()
11
Synchronous Method: int GetInt() { int number = GetNumber(); DoIndependentWork(); return number; } Asynchronous Method: async Task GetIntAsync() { Task getNumTask = GetNumberAsync(); DoIndependentWork(); int number = await getNumTask return number; }
12
Mark async method with Async or async Marked method can use Await or await
13
async Task AccessTheWebAsync() { HttpClient client = new HttpClient(); Task getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); DoIndependentWork(); string urlContents = await getStringTask; return urlContents.Length; }
14
Source: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx
15
http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4- 5-worth-the-await.aspx http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4- 5-worth-the-await.aspx http://www.asp.net/web-forms/tutorials/aspnet-45/using- asynchronous-methods-in-aspnet-45 http://www.asp.net/web-forms/tutorials/aspnet-45/using- asynchronous-methods-in-aspnet-45 http://www.dotnetperls.com/async http://www.dotnetperls.com/async http://www.codeproject.com/Tips/591586/Asynchronous- Programming-in-Csharp-5-0-using-async http://www.codeproject.com/Tips/591586/Asynchronous- Programming-in-Csharp-5-0-using-async
16
Asynchronous ◦ Multi-Threaded
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.