The Labs \ Source Viewer \ SSCLI \ System.Runtime.Remoting.Messaging \ AsyncResult

  1. // ==++==
  2. //
  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. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Interface: AsyncResult
  18. **
  19. ** Purpose: Object to encapsulate the results of an async
  20. **          operation
  21. **
  22. ===========================================================*/
  23. namespace System.Runtime.Remoting.Messaging
  24. {
  25.     using System.Threading;
  26.     using System.Runtime.Remoting;
  27.     using System;
  28.     using System.Security.Permissions;
  29.    
  30.     [System.Runtime.InteropServices.ComVisible(true)]
  31.     public class AsyncResult : IAsyncResult, IMessageSink
  32.     {
  33.        
  34.         internal AsyncResult(Message m)
  35.         {
  36.             m.GetAsyncBeginInfo(out _acbd, out _asyncState);
  37.             _asyncDelegate = (Delegate)m.GetThisPtr();
  38.         }
  39.        
  40.        
  41.         // True if the asynchronous operation has been completed.
  42.         public virtual bool IsCompleted {
  43.             get { return _isCompleted; }
  44.         }
  45.         // The delegate object on which the async call was invoked.
  46.         public virtual object AsyncDelegate {
  47.             get { return _asyncDelegate; }
  48.         }
  49.        
  50.        
  51.         // The state object passed in via BeginInvoke.
  52.         public virtual object AsyncState {
  53.             get { return _asyncState; }
  54.         }
  55.        
  56.        
  57.         public virtual bool CompletedSynchronously {
  58.             get { return false; }
  59.         }
  60.        
  61.         public bool EndInvokeCalled {
  62.             get { return _endInvokeCalled; }
  63.             set {
  64.                 BCLDebug.Assert(!_endInvokeCalled && value, "EndInvoke prevents multiple calls");
  65.                
  66.                 _endInvokeCalled = value;
  67.             }
  68.         }
  69.        
  70.         private void FaultInWaitHandle()
  71.         {
  72.             lock (this) {
  73.                 if (_AsyncWaitHandle == null) {
  74.                     _AsyncWaitHandle = new ManualResetEvent(false);
  75.                 }
  76.             }
  77.         }
  78.        
  79.         public virtual WaitHandle AsyncWaitHandle {
  80.             get {
  81.                 FaultInWaitHandle();
  82.                 return _AsyncWaitHandle;
  83.             }
  84.         }
  85.        
  86.         public virtual void SetMessageCtrl(IMessageCtrl mc)
  87.         {
  88.             _mc = mc;
  89.         }
  90.        
  91.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  92.         public virtual IMessage SyncProcessMessage(IMessage msg)
  93.         {
  94.             if (msg == null) {
  95.                 _replyMsg = new ReturnMessage(new RemotingException(Environment.GetResourceString("Remoting_NullMessage")), new ErrorMessage());
  96.             }
  97.             else if (!(msg is IMethodReturnMessage)) {
  98.                 _replyMsg = new ReturnMessage(new RemotingException(Environment.GetResourceString("Remoting_Message_BadType")), new ErrorMessage());
  99.             }
  100.             else {
  101.                 _replyMsg = msg;
  102.             }
  103.            
  104.             _isCompleted = true;
  105.             FaultInWaitHandle();
  106.            
  107.             _AsyncWaitHandle.Set();
  108.             if (_acbd != null) {
  109.                 // NOTE: We are invoking user code here!
  110.                 // Catch and Ignore exceptions thrown from async callback user code.
  111.                 _acbd(this);
  112.             }
  113.             return null;
  114.         }
  115.        
  116.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  117.         public virtual IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
  118.         {
  119.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_Method"));
  120.         }
  121.        
  122.         public IMessageSink NextSink {
  123.             [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  124.             get { return null; }
  125.         }
  126.        
  127.         public virtual IMessage GetReplyMessage()
  128.         {
  129.             return _replyMsg;
  130.         }
  131.        
  132.         private IMessageCtrl _mc;
  133.         private AsyncCallback _acbd;
  134.         private IMessage _replyMsg;
  135.         private bool _isCompleted;
  136.         private bool _endInvokeCalled;
  137.         private ManualResetEvent _AsyncWaitHandle;
  138.         private Delegate _asyncDelegate;
  139.         private object _asyncState;
  140.     }
  141. }

Developer Fusion