Presentation is loading. Please wait.

Presentation is loading. Please wait.

Asynchronous programming Deadlock All The Things!.

Similar presentations


Presentation on theme: "Asynchronous programming Deadlock All The Things!."— Presentation transcript:

1 Asynchronous programming Deadlock All The Things!

2 Page Filip Ekberg +61 (401) C# Smorgasbord / Copyright ©2014 by Readify Pty Ltd2

3 Page Asynchronous programming? / Copyright ©2014 by Readify Pty Ltd3 ›Processing allowed before current execution is done, such as read/write to disk ›So is this the same as parallel? Not

4 Page Async + Parallel / Copyright ©2014 by Readify Pty Ltd4 12:38 Load app content Sort data Process records Asynchronous

5 Page Async? Never heard of it. / Copyright ©2014 by Readify Pty Ltd5 ›Ajax - Asynchronous JavaScript and XML ›Request data ›Handle the response when

6 Page Introducing Async and Await / Copyright ©2014 by Readify Pty Ltd6 ›Two new Contextual Keywords as of.NET 4.5 ›Meaning it does all the hard work for you public async Task RunAsync() { await Task.Run(() => { });

7 Page Marking a method as Async / Copyright ©2014 by Readify Pty Ltd7 ›This will indicate that the method will be executed from within a state machine ›Adds overhead ›If it is not actually awaiting something, don’t mark it as async! ›Simply marking as async doesn’t make it

8 Page What do you think happens? / Copyright ©2014 by Readify Pty Ltd8 private void Button_Click() { try { Run(); } catch (Exception ex) { } public void Run() { throw new Exception();

9 Page What do you think happens? / Copyright ©2014 by Readify Pty Ltd9 private void Button_Click() { try { RunAsync(); } catch (Exception ex) { } public async void RunAsync() { throw new Exception();

10 Page The second one crashes! / Copyright ©2014 by Readify Pty Ltd10 ›Async void is EVIL! ›Marking a method as async wraps it inside a state machine [AsyncStateMachine(typeof( d__0)), DebuggerStepThrough] public void RunAsync2() { d__0 d__; d__.<>4__this = this; d__.<>t__builder = AsyncVoidMethodBuilder.Create(); d__.<>1__state = -1; d__.<>t__builder.Start d__0>(ref d__);

11 Page How do we handle this? / Copyright ©2014 by Readify Pty Ltd11 ›Use Task as a return type instead ›Always await your async calls private async void Button_Click() { try { await RunAsync(); } catch (Exception ex) { } private void Button_Click() { RunAsync(); } public async Task RunAsync() { throw new Exception(); } Silently fails “Validates” and lets you catch any possible

12 Page Proper Async method / Copyright ©2014 by Readify Pty Ltd12 public async Task BoilEggsAsync(int amountOfEggs) { await BoilAsync(amountOfEggs); } public Task BoilEggsAsync(int amountOfEggs) { d__5 d__; d__.<>4__this = this; d__.amountOfEggs = amountOfEggs; d__.<>t__builder = AsyncTaskMethodBuilder.Create(); d__.<>1__state = -1; d__.<>t__builder.Start d__5>(ref d__); return d__.<>t__builder.Task; }

13 Page Awaiting a result / Copyright ©2014 by Readify Pty Ltd13 ›Awaiting a task will schedule a continuation block ›Everything after the awaiting task will be executed when the task is done executing public async Task RunAsync() { var resultTask = Task.Run(() => { Thread.Sleep(2000); return "Hello World!"; }); var result = await resultTask; } Back on the UI thread after the waiting is

14 Page Example of deadlocking / Copyright ©2014 by Readify Pty Ltd14 ›Wait() for an asynchronous task on the UI thread, making it impossible for the state machine to signal that it is completed! public async Task RunAsync()…

15 Page Things to consider / Copyright ©2014 by Readify Pty Ltd15 ›Don’t mark void methods as asynchronous, you will have no way of tracking it ›Tasks swallow Exceptions! ›Don’t explicitly.Wait() this is an easy path to deadlocking ›Follow the naming convention MyMethodAsync ›Don’t lie by wrapping synchronous/blocking code in async methods ›The continuation is on the calling thread! ›Async async () => { await Task.Delay(2000); }

16 Page / Copyright ©2014 by Readify Pty

17 Page Summary / Copyright ©2014 by Readify Pty Ltd17 ›Async is powerful ›Using Async wrong may deadlock your application ›Can be hard to debug ›Await all your tasks to verify the

18 Page Questions? / Copyright ©2014 by Readify Pty Ltd18

19 Page Thank you! +61 (401) C# Smorgasbord / Copyright ©2014 by Readify Pty Ltd19


Download ppt "Asynchronous programming Deadlock All The Things!."

Similar presentations


Ads by Google