DEV301
// Synchronous TResult Foo(...); // Asynchronous Programming Model (APM) IAsyncResult BeginFoo(..., AsyncCallback callback, object state); TResult EndFoo(IAsyncResult asyncResult); // Event-based Asynchronous Pattern (EAP) public void FooAsync(...); public event EventHandler FooCompleted;
public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }
public IAsyncResult BeginCopyStreamToStream( 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(null); return; } iar = destination.BeginWrite(buffer, 0, numRead, writeResult => { if (writeResult.CompletedSynchronously) return; destination.EndWrite(writeResult); readWriteLoop(null); }, null); if (!iar.CompletedSynchronously) return; destination.EndWrite(iar); break; } } } catch (Exception e) { tcs.TrySetException(e); } }; readWriteLoop(null); return tcs.Task; } public void EndCopyStreamToStream(IAsyncResult asyncResult) { ((Task)asyncResult).Wait(); }
public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } } public async Task CopyStreamToStreamAsync(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); } }
// Synchronous TResult Foo(...); // Asynchronous Programming Model (APM) IAsyncResult BeginFoo(..., AsyncCallback callback, object state); TResult EndFoo(IAsyncResult asyncResult); // Event-based Asynchronous Pattern (EAP) public void FooAsync(...); public event EventHandler FooCompleted; // Task-based Asynchronous Pattern (TAP) Task FooAsync(...); System.IO.Stream: Task ReadAsync(...); Task WriteAsync(...); Task FlushAsync(); Task CopyToAsync(...);
demo
ActionBlock Message Queue Process(0);Process(1);Process(2);Process(3); 4 4 Process(4);
IDataflowBlock ISourceBlock (a source of data) ITargetBlock (a target for data) IPropagatorBlock<> (a source and target) Built-in blocks for Buffering and PropagationBuilt-in blocks for ExecutingBuilt-in blocks for Joining
DataflowBlockOptions TaskScheduler MaxMessagesPerTask CancellationToken BoundedCapacity ExecutionDataflowBlockOptions MaxDegreeOfParallelism GroupingDataflowBlockOptions Greedy MaxNumberOfGroups
Sessions On-Demand & CommunityMicrosoft Certification & Training Resources Resources for IT ProfessionalsResources for Developers Connect. Share. Discuss.
Scan the Tag to evaluate this session now on myTechEd Mobile