23
No More Deadlocks Filip Ekberg

No More Deadlocks; Asynchronous Programming in .NET

Embed Size (px)

Citation preview

Page 1: No More Deadlocks; Asynchronous Programming in .NET

No More DeadlocksFilip Ekberg

Page 2: No More Deadlocks; Asynchronous Programming in .NET

I’m Filip Ekberg

Readify

@fekberg

Author. Blogger. Speaker. .NET MVP. Xamarin MVP. Geek.

Senior Software Engineer @

Page 3: No More Deadlocks; Asynchronous Programming in .NET

Asynchronous Programming?

• Processing allowed before current execution is done, such as read/write to disk

• Asynchronous JavaScript (AJAX)

@fekberg

Page 4: No More Deadlocks; Asynchronous Programming in .NET

Why are we talking about this now?• Not a new concept• Becoming more and more common

@fekberg

• Web Apps• Mobile Apps• Desktop Apps

Page 5: No More Deadlocks; Asynchronous Programming in .NET

Avoid Unreliable Applications!

• Don’t crash (doh!)• Don’t surprise the users• Show a loading indicator

12:38

@fekberg

Page 6: No More Deadlocks; Asynchronous Programming in .NET

Not the same as Parallel Programming

@fekberg

Page 7: No More Deadlocks; Asynchronous Programming in .NET

From Sync to Async

@fekberg

Page 8: No More Deadlocks; Asynchronous Programming in .NET

From Blocking to Non-Blocking

• Free the main/UI thread quickly• Don’t force a block because async is

difficult

@fekberg

Page 9: No More Deadlocks; Asynchronous Programming in .NET

Task Parallel Library (TPL)

Simplify the work of writing concurrent and asynchronous code

@fekberg

Page 10: No More Deadlocks; Asynchronous Programming in .NET

Refresh my mind!

@fekberg

var task = Task.Run(() => {

Thread.Sleep(2000);

return "Hello World!";});

task.ContinueWith((completedTask) => { Dispatcher.Invoke(() => MyButton.Content = completedTask.Result);});

Continuation not on UI Thread!

Invoke on the UI Thread!

Page 11: No More Deadlocks; Asynchronous Programming in .NET

Easy Deadlocking

@fekberg

Task.Delay(1).ContinueWith((t) => { Dispatcher.Invoke(() => { });}).Wait();

Block the UI Thread!Invoke on the UI Thread!

Page 12: No More Deadlocks; Asynchronous Programming in .NET

Introducing Async and Await

@fekberg

Page 13: No More Deadlocks; Asynchronous Programming in .NET

New Contextual Keywords

Async and Await were introduced in .NET 4.5

• Hides a lot of complexity behind the scenes

• Makes code more readable• Makes code more fragile

@fekberg

Page 14: No More Deadlocks; Asynchronous Programming in .NET

Does this apply to ASP.NET?

• Yes!• But.. The caller will know no difference

@fekberg

Page 15: No More Deadlocks; Asynchronous Programming in .NET

What about Xamarin?

• Scheduling might differ per platform• Potentially easier to deadlock!

@fekberg

Page 16: No More Deadlocks; Asynchronous Programming in .NET

Demo

Using Async and Await

@fekberg

Page 17: No More Deadlocks; Asynchronous Programming in .NET

Behind the Scenes

@fekberg

Page 18: No More Deadlocks; Asynchronous Programming in .NET

Example

@fekberg

private async Task RunAsync(){ var x = 10;

await Task.Delay(2000);

Debug.WriteLine(x);}

Page 19: No More Deadlocks; Asynchronous Programming in .NET

What about RunAsync’s method body?

@fekberg

private Task RunAsync(){ <RunAsync>d__1 stateMachine = new <RunAsync>d__1 { <>t__builder = AsyncTaskMethodBuilder.Create(), <>1__state = -1 }; stateMachine.<>t__builder.Start<<RunAsync>d__1>(ref stateMachine); return stateMachine.<>t__builder.Task;}

Page 20: No More Deadlocks; Asynchronous Programming in .NET

Demo

Deadlock all the things!

@fekberg

Page 21: No More Deadlocks; Asynchronous Programming in .NET

Considerations

• Don’t mark void methods as asynchronous methods

• Tasks swallow Exceptions!• 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!

@fekberg

Page 22: No More Deadlocks; Asynchronous Programming in .NET

Questions?

Readify

@fekberg

Page 23: No More Deadlocks; Asynchronous Programming in .NET

Thank you,I’m Filip Ekberg

Readify

@fekberg

Author. Blogger. Speaker. .NET MVP. Xamarin MVP. Geek.

Senior Software Engineer @