Skip to content

Commit 4252932

Browse files
committed
improved no deadlock example
1 parent d7ac748 commit 4252932

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

AsyncTesting.sln

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.10
4+
VisualStudioVersion = 15.0.26730.16
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFGui", "AsyncTesting\WPFGui.csproj", "{201924C3-E71B-4F00-9F77-9024E7D90DF9}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AB0737E7-61A4-49A7-A2C5-F89080031747}"
9+
ProjectSection(SolutionItems) = preProject
10+
.gitignore = .gitignore
11+
EndProjectSection
12+
EndProject
813
Global
914
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1015
Debug|Any CPU = Debug|Any CPU

AsyncTesting/Commands/SaveAndDeadLockEvenWithConfigureAwaitFalse.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ public void Execute(object parameter)
2424
TheViewModel vm = parameter as TheViewModel;
2525
vm.CurrentThreadID = Thread.CurrentThread.ManagedThreadId;
2626

27-
//Blocks while CallSomethingAsync runs. CallSomethingAsync
28-
//has switched contexts even though ConfigureAwait(false)
29-
//is used! This is because the library code does not also
30-
//use ConfigureAwait(false);
27+
//Blocks while CallSomethingAsync runs.
3128
string msg = CallSomethingAsync(vm).Result;
3229
vm.CurrentThreadID = Thread.CurrentThread.ManagedThreadId;
3330
vm.UpdateDescription(msg);

AsyncTesting/Commands/SaveWithNoDeadlock.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using AsyncTesting.Models;
22
using System;
3+
using System.Diagnostics;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using System.Windows.Input;
7+
using System.Windows.Threading;
68

79
namespace AsyncTesting.Commands
810
{
@@ -26,14 +28,14 @@ public void Execute(object parameter)
2628

2729
//Blocks while CallSomethingAsync runs. CallSomethingAsync
2830
//cannot resume on the context because .Result is blocking.
29-
string msg = CallSomethingAsync(vm).Result;
30-
vm.CurrentThreadID = Thread.CurrentThread.ManagedThreadId;
31-
vm.UpdateDescription(msg);
31+
var msg = CallSomethingAsync(vm);
32+
vm.UpdateDescription(msg.Result);
3233
}
3334

3435
private async Task<string> CallSomethingAsync(TheViewModel vm)
3536
{
36-
await Task.Delay(10).ConfigureAwait(continueOnCapturedContext:false);
37+
//quick delay just to (attempt) to switch threads
38+
await Task.Delay(10).ConfigureAwait(continueOnCapturedContext:false);
3739
vm.CurrentThreadID = Thread.CurrentThread.ManagedThreadId;
3840
await Task.Delay(1000).ConfigureAwait(continueOnCapturedContext: false);
3941
return $"No deadlock because blocking call is on different context.";

0 commit comments

Comments
 (0)