@@ -53,23 +53,37 @@ public SynchronizationContext SynchronizationContext
53
53
54
54
public void Post ( SendOrPostCallback d , object arg )
55
55
{
56
- VerifyNotCompleted ( ) ;
57
- VerifyDelegateNotNull ( d ) ;
58
- _syncContext . Post ( d , arg ) ;
56
+ PostCore ( d , arg , markCompleted : false ) ;
59
57
}
60
58
61
59
public void PostOperationCompleted ( SendOrPostCallback d , object arg )
62
60
{
63
- Post ( d , arg ) ;
61
+ PostCore ( d , arg , markCompleted : true ) ;
64
62
OperationCompletedCore ( ) ;
65
63
}
66
64
67
65
public void OperationCompleted ( )
68
66
{
69
67
VerifyNotCompleted ( ) ;
68
+ _alreadyCompleted = true ;
70
69
OperationCompletedCore ( ) ;
71
70
}
72
71
72
+ private void PostCore ( SendOrPostCallback d , object arg , bool markCompleted )
73
+ {
74
+ VerifyNotCompleted ( ) ;
75
+ VerifyDelegateNotNull ( d ) ;
76
+ if ( markCompleted )
77
+ {
78
+ // This call is in response to a PostOperationCompleted. As such, we need to mark
79
+ // _alreadyCompleted as true so that subsequent attempts to use this instance will
80
+ // fail appropriately. And we need to do that before we queue the callback, as
81
+ // that callback could itself trigger additional attempts to use this instance.
82
+ _alreadyCompleted = true ;
83
+ }
84
+ _syncContext . Post ( d , arg ) ;
85
+ }
86
+
73
87
private void OperationCompletedCore ( )
74
88
{
75
89
try
@@ -78,7 +92,6 @@ private void OperationCompletedCore()
78
92
}
79
93
finally
80
94
{
81
- _alreadyCompleted = true ;
82
95
GC . SuppressFinalize ( this ) ;
83
96
}
84
97
}
0 commit comments