Jump to content

Asynchronous method invocation

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Davhdavh (talk | contribs) at 08:18, 8 October 2008 (Created page with 'The '''Asyncronious error reporting''' design pattern decouples exception throwing from their origin to the use of the result, in such a way that exceptions hap...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

The Asyncronious error reporting design pattern decouples exception throwing from their origin to the use of the result, in such a way that exceptions happen in a "safe" way. Often used in connection with the Active Object pattern Active Object of control.

Recently seen implemented as a basic principle in Microsoft Silverlight.

C# example

public class SomethingCallbackResult
{
   private object _result;
   public object Result {
      get {
          //Exception is not thrown until we ask for result
          if (_result is Exception)
             throw (Exception)_result;
          return _result;
      }
      set {
         _result = value;
      }
   }
}

public class Something
{
   public delegate void SomethingCallback(SomethingCallbackResult result);

   public void BeginSomething(SomethingCallback callback)
   {
      //Start thread with worker

      //return
   }

   private void worker(SomethingCallback callback)
   {
      SomethingCallbackResult result = new SomethingCallbackResult();
      try
      {
          //... Actual work ...
          result.Result = ...
      }
      catch (Exception e)
      {
         //Exception is not throw here, but just saved for later use
         result.Result = e;
      }
      callback(result);
   }
}