Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sync with async

513 views

Published on

Asynchronous programming

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Sync with async

  1. 1. Sync with Async M Prabath Maduranga Peiris MSP / MSA RUSL
  2. 2. Why Async ? Async allows programmer to avoid performance bottlenecks and improve applications responsiveness. Scenario : Applications access the HTTP (Web ) inside the program Access the web is slow and delayed Total application will be wait Sync With Async - PrabathSL
  3. 3. Sync With Async - PrabathSL private void uploadImageCallbackHell() { bool BufferStatus = true; var filePicker = new FilePicker(); var PickerTask = filePicker.PickImage(); PickerTask.ContinueWith((PickerReturn) => { if (PickerTask.IsCanceled) { BufferStatus = false; } else { var Uploadtask = new Task<bool>(() => { return sendUpdate(PickerReturn); }); Uploadtask.ContinueWith((UploadReturnTask)()=> { BufferStatus = false; }); } } ); }
  4. 4. With Async Application can continue with other works that doesn’t depend on blocked resource until the blocked task finished. Sync With Async - PrabathSL
  5. 5. Async Async programming available with Visual Studio 2012 and .NET framework 4.5 onwards Runtime components support Async: Web ( HttpClient / SyndicationClient ) File Access ( StorageFile / StreamWriter / XmlReader ) Images ( MediaCapture / BitMapEncoder and Decoder ) Sync With Async - PrabathSL
  6. 6. Concepts in Async Sync With Async - PrabathSL
  7. 7. Tasks Reasoning about Background operations Task and Task<T> Encapsulate units of works State : Running , Finished , Cancelled Result Thrown Exceptions Sync With Async - PrabathSL
  8. 8. Working With Tasks Useful Operations Wait : Task.WaitAny(t1,t2,t3) Wait : Task.WaitAll(t1,t1,t3) Chain : task1.ContinueWith(task2) Wrap : Task.WhenAny(t1,t2,t3) Wrap : Task.WhenAll(t1,t2,t3) Sync With Async - PrabathSL
  9. 9. Async Support Base Class Libraries (BCL) All standard Async API’s from .NET BCL Coverage is .NET 4.5 Complete for BCL What is available on Async Any API that take more than 50ms to run Sync With Async - PrabathSL
  10. 10. Compiler is Amazing async key word informs the compiler that this method need to be handle with it self await indicates a suspension point where callback need to be generated Continuations are generated after each suspension point Exception handling with Try Catch Remember async and await , then use Tasks Sync With Async - PrabathSL
  11. 11. How to use Async ? private async void ButtonDefinition() { Async modifier can be on , Methods Lambdas Anonymous methods button.Click += async (sender, e) =>{ int number = await getNumberAsync(); Use Coding standards Use Async Suffix | e.g. DownloadAsync Return Task or Task<T> according to situation void for only event handlers Sync With Async - PrabathSL }; Task<int> itemNumberTask = getNumberAsync(); int num = await itemNumberTask; } private async Task<int> getNumberAsync() { await Task.Delay(200); return 10; }
  12. 12. Demo Sync With Async - PrabathSL
  13. 13. Async Execution 1 2 3 private void independentWork() Sync With Async - PrabathSL
  14. 14. Advanced Tips Working with interdependent Async Operations ?? Mutual exclusion Shared flag Sleep and Wakeup Mutex Semaphore Sync With Async - PrabathSL
  15. 15. Advanced Tips Task manages set of class Task.WhenAll(Ienumerable<Task>) Require all class to complete Task.WhenAny(Ienumerable<Task>) Require any one of task completed private async Task BulkTaskOperationAsync() Sync With Async - PrabathSL { var t1 = await Task.Factory.StartNew(() => getNumberAsync()); var t2 = await Task.Factory.StartNew(() => getNumberAsync()); Task.WaitAny(t1, t2); }
  16. 16. Questions Sync With Async - PrabathSL
  17. 17. Thank You Blog : prabathsl.blogspot.com Email : prabathsl@outlook.com Fb/ Twitter : @mpmpeiris Sync With Async - PrabathSL

×