Presentation is loading. Please wait.

Presentation is loading. Please wait.

async void button1_Click(…) { … await ScheduleAsync(work); … } async Task ScheduleAsync(Action work) { work(); } async void button1_Click(…) { … await.

Similar presentations


Presentation on theme: "async void button1_Click(…) { … await ScheduleAsync(work); … } async Task ScheduleAsync(Action work) { work(); } async void button1_Click(…) { … await."— Presentation transcript:

1

2

3 async void button1_Click(…) { … await ScheduleAsync(work); … } async Task ScheduleAsync(Action work) { work(); } async void button1_Click(…) { … await Task.Run(() => work()); … }

4 …; Task.Delay(1000); …; …; await Task.Delay(1000); …; Parallel.For(0, 10, async i => { await Task.Delay(1000); }); await Task.Factory.StartNew( async () => { await Task.Delay(1000); }); await Task.Run( async () => { await Task.Delay(1000); });

5 var tcs = new TaskCompletionSource (); Task.Run(delegate { T result = Foo(); tcs.SetResult(result); }); return tcs.Task; var tcs = new TaskCompletionSource (); Task.Run(delegate { try { T result = Foo(); tcs.SetResult(result); } catch(Exception e) { tcs.SetException(e); } }); return tcs.Task; void button1_Click(…) { FooAsync().Wait(); } async Task FooAsync() { await Task.Delay(1000); } async void button1_Click(…) { await FooAsync(); } async Task FooAsync() { await Task.Delay(1000); } async void button1_Click(…) { await FooAsync(); } async Task FooAsync() { await Task.Delay(1000).ConfigureAwait(false); }

6

7 async Task LotsOfWorkAsync() { var throttle = Throttle (async message => { await Task.Yield(); Console.WriteLine(message);}, maxParallelism: Environment.ProcessorCount); throttle.Post("lots"); throttle.Post("of"); throttle.Post("work"); throttle.Complete(); // Signal that we're done enqueuing work. await throttle.Completion; } static ITargetBlock Throttle (Func worker, int max) { var opts = new ExecutionDataflowBlockOptions() {MaxDegreeOfParallelism = max}; return new ActionBlock (worker, opts); }

8 public async Task > LoadHousesAsync(int first, int last) { var loadedHouses = new List (); var queue = new Queue (Enumerable.Range(first, last – first + 1)); // Throttle the rate of issuing requests... var worker1 = WorkerAsync(queue, loadedHouses); var worker2 = WorkerAsync(queue, loadedHouses); var worker3 = WorkerAsync(queue, loadedHouses); await Task.WhenAll(worker1, worker2, worker3); return loadedHouses; } private async Task WorkerAsync(Queue queue, List results) { while (queue.Count > 0) { int i = queue.Dequeue(); var house = await House.LoadFromDatabaseAsync(i); results.Add(house); } private async Task WorkerAsync(Queue queue, List results) { while (queue.Count > 0) { int i = queue.Dequeue(); var house = await House.LoadFromDatabaseAsync(i); results.Add(house); }

9 Explicitly Controlled await Task.WhenAll(from i in Enumerable.Range(0, N) select Task.Run(async delegate { … })); SemaphoreSlim var sem = new SemaphoreSlim(N); await sem.WaitAsync(); …; sem.Release(); ConcurrentExclusiveSchedulerPair var f = new TaskFactory( new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, N).ConcurrentScheduler); f.StartNew(…); // note: only counts tasks currently on a thread ActionBlock var ab = new ActionBlock (async t => …, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = N });

10 Signature Task XxAsync( T1 Arg1, T2 Arg2, CancellationToken cancellationToken, IProgress progress); Returns Task or Task Async suffix Parameters matching synchronous method (no ref or out) Cancellation/progress parameters only if supported Behavior Returns quickly to caller Returned Task must be hot May complete synchronously Exceptions stored in returned Task (only usage exceptions allowed to escape synchronously)

11 public static Task PausePrintAsync() { // Requires a thread for all // of its processing return Task.Run(() => { Thread.Sleep(10000); Console.WriteLine("Hello"); }); } public static Task PausePrintAsync() { // Dont consume threads // when no threads are needed var t = Task.Delay(10000); return t.ContinueWith(_ => { Console.WriteLine("Hello")); }); } Task await task;task.Wait(); Synchronous join Asynchronous join


Download ppt "async void button1_Click(…) { … await ScheduleAsync(work); … } async Task ScheduleAsync(Action work) { work(); } async void button1_Click(…) { … await."

Similar presentations


Ads by Google