The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ AsyncOperation

  1. //------------------------------------------------------------------------------
  2. // <copyright file="AsyncOperation.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.ComponentModel
  16. {
  17.     using System.Security.Permissions;
  18.     using System.Threading;
  19.    
  20.     [HostProtection(SharedState = true)]
  21.     public sealed class AsyncOperation
  22.     {
  23.         private SynchronizationContext syncContext;
  24.         private object userSuppliedState;
  25.         private bool alreadyCompleted;
  26.        
  27.         /// <summary>
  28.         /// Constructor. Protected to avoid unwitting usage - AsyncOperation objects
  29.         /// are typically created by AsyncOperationManager calling CreateOperation.
  30.         /// </summary>
  31.         private AsyncOperation(object userSuppliedState, SynchronizationContext syncContext)
  32.         {
  33.             this.userSuppliedState = userSuppliedState;
  34.             this.syncContext = syncContext;
  35.             this.alreadyCompleted = false;
  36.             this.syncContext.OperationStarted();
  37.         }
  38.        
  39.         /// <summary>
  40.         /// Destructor. Guarantees that sync context will always get notified of completion.
  41.         /// </summary>
  42.         ~AsyncOperation()
  43.         {
  44.             if (!alreadyCompleted && syncContext != null) {
  45.                 syncContext.OperationCompleted();
  46.             }
  47.         }
  48.        
  49.         public object UserSuppliedState {
  50.             get { return userSuppliedState; }
  51.         }
  52.        
  53.         /// <include file='doc\AsyncOperation.uex' path='docs/doc[@for="AsyncOperation.SynchronizationContext"]/*' />
  54.         public SynchronizationContext SynchronizationContext {
  55.             get { return syncContext; }
  56.         }
  57.        
  58.         public void Post(SendOrPostCallback d, object arg)
  59.         {
  60.             VerifyNotCompleted();
  61.             VerifyDelegateNotNull(d);
  62.             syncContext.Post(d, arg);
  63.         }
  64.        
  65.         public void PostOperationCompleted(SendOrPostCallback d, object arg)
  66.         {
  67.             Post(d, arg);
  68.             OperationCompletedCore();
  69.         }
  70.        
  71.         public void OperationCompleted()
  72.         {
  73.             VerifyNotCompleted();
  74.             OperationCompletedCore();
  75.         }
  76.        
  77.         private void OperationCompletedCore()
  78.         {
  79.             try {
  80.                 syncContext.OperationCompleted();
  81.             }
  82.             finally {
  83.                 alreadyCompleted = true;
  84.                 GC.SuppressFinalize(this);
  85.             }
  86.         }
  87.        
  88.         private void VerifyNotCompleted()
  89.         {
  90.             if (alreadyCompleted) {
  91.                 throw new InvalidOperationException(SR.GetString(SR.Async_OperationAlreadyCompleted));
  92.             }
  93.         }
  94.        
  95.         private void VerifyDelegateNotNull(SendOrPostCallback d)
  96.         {
  97.             if (d == null) {
  98.                 throw new ArgumentNullException(SR.GetString(SR.Async_NullDelegate), "d");
  99.             }
  100.         }
  101.        
  102.         /// <summary>
  103.         /// Only for use by AsyncOperationManager to create new AsyncOperation objects
  104.         /// </summary>
  105.         static internal AsyncOperation CreateOperation(object userSuppliedState, SynchronizationContext syncContext)
  106.         {
  107.             AsyncOperation newOp = new AsyncOperation(userSuppliedState, syncContext);
  108.             return newOp;
  109.         }
  110.     }
  111. }

Developer Fusion