Cole Durdan
What is asynchronous programming? Previous patterns Task Based Async Programming .NET 4.5 Keywords What happens in an async. method? Demo
“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------|
Web Access Database Requests Working with files Working with images WCF programming Working with sockets With UI that need to be responsive
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
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(); }
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); } }
A lot of extra code Difficult to… ◦ Read ◦ Write ◦ Maintain ◦ Debug
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
Keywords ◦ “Async” ◦ “Await” Return Type of Object Task ◦ Task if sub or void ◦ Task Naming Convention ◦ MethodNameAsync()
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; }
Mark async method with Async or async Marked method can use Await or await
async Task AccessTheWebAsync() { HttpClient client = new HttpClient(); Task getStringTask = client.GetStringAsync(" DoIndependentWork(); string urlContents = await getStringTask; return urlContents.Length; }
Source:
5-worth-the-await.aspx 5-worth-the-await.aspx asynchronous-methods-in-aspnet-45 asynchronous-methods-in-aspnet-45 Programming-in-Csharp-5-0-using-async Programming-in-Csharp-5-0-using-async
Asynchronous ◦ Multi-Threaded