mscorlib(4.0.0.0) API with additions
AsyncOperation.cs
2 using System.Threading;
3 
4 namespace System.ComponentModel
5 {
7  [global::__DynamicallyInvokable]
8  [HostProtection(SecurityAction.LinkDemand, SharedState = true)]
9  public sealed class AsyncOperation
10  {
11  private SynchronizationContext syncContext;
12 
13  private object userSuppliedState;
14 
15  private bool alreadyCompleted;
16 
19  [global::__DynamicallyInvokable]
20  public object UserSuppliedState
21  {
22  [global::__DynamicallyInvokable]
23  get
24  {
25  return userSuppliedState;
26  }
27  }
28 
31  [global::__DynamicallyInvokable]
33  {
34  [global::__DynamicallyInvokable]
35  get
36  {
37  return syncContext;
38  }
39  }
40 
41  private AsyncOperation(object userSuppliedState, SynchronizationContext syncContext)
42  {
43  this.userSuppliedState = userSuppliedState;
44  this.syncContext = syncContext;
45  alreadyCompleted = false;
46  this.syncContext.OperationStarted();
47  }
48 
50  ~AsyncOperation()
51  {
52  if (!alreadyCompleted && syncContext != null)
53  {
54  syncContext.OperationCompleted();
55  }
56  }
57 
64  [global::__DynamicallyInvokable]
65  public void Post(SendOrPostCallback d, object arg)
66  {
67  VerifyNotCompleted();
68  VerifyDelegateNotNull(d);
69  syncContext.Post(d, arg);
70  }
71 
79  [global::__DynamicallyInvokable]
80  public void PostOperationCompleted(SendOrPostCallback d, object arg)
81  {
82  Post(d, arg);
83  OperationCompletedCore();
84  }
85 
89  [global::__DynamicallyInvokable]
90  public void OperationCompleted()
91  {
92  VerifyNotCompleted();
93  OperationCompletedCore();
94  }
95 
96  private void OperationCompletedCore()
97  {
98  try
99  {
100  syncContext.OperationCompleted();
101  }
102  finally
103  {
104  alreadyCompleted = true;
105  GC.SuppressFinalize(this);
106  }
107  }
108 
109  private void VerifyNotCompleted()
110  {
111  if (alreadyCompleted)
112  {
113  throw new InvalidOperationException(SR.GetString("Async_OperationAlreadyCompleted"));
114  }
115  }
116 
117  private void VerifyDelegateNotNull(SendOrPostCallback d)
118  {
119  if (d == null)
120  {
121  throw new ArgumentNullException(SR.GetString("Async_NullDelegate"), "d");
122  }
123  }
124 
125  internal static AsyncOperation CreateOperation(object userSuppliedState, SynchronizationContext syncContext)
126  {
127  return new AsyncOperation(userSuppliedState, syncContext);
128  }
129  }
130 }
void OperationCompleted()
Ends the lifetime of an asynchronous operation.
virtual void Post(SendOrPostCallback d, object state)
When overridden in a derived class, dispatches an asynchronous message to a synchronization context.
Tracks the lifetime of an asynchronous operation.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
Definition: __Canon.cs:3
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides the basic functionality for propagating a synchronization context in various synchronization...
virtual void OperationCompleted()
When overridden in a derived class, responds to the notification that an operation has completed.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
void Post(SendOrPostCallback d, object arg)
Invokes a delegate on the thread or context appropriate for the application model.
object UserSuppliedState
Gets or sets an object used to uniquely identify an asynchronous operation.
void PostOperationCompleted(SendOrPostCallback d, object arg)
Ends the lifetime of an asynchronous operation.
virtual void OperationStarted()
When overridden in a derived class, responds to the notification that an operation has started.