C#: Asynchronous Pattern

Asynchronous Pattern

The underlining philosophy is as follows:
– It is the client that decides if a particular call should be asynchronous.
– It is not necessary for a server to do additional programming; The CLI should be able to manage the difference between the client and server views.
– The server can choose to explicitly support asynchronous behavior either because it can implement it more efficiently than a general architecture, or it wants to support only asynchronous behavior.
– It is supposed to enforce type safety.
– The CLI will provide a list of necessary services to support the asynchronous programming model:
  – Synchronization primitives, such as critical sections and ReaderWriterLock.
  – Synchronization constructs, such as containers that supports WaitForMultipleObjects method.
  – Exposure to the underlying infrastructure pieces, such as Message objects and Thread pools.

– The object that implements IAsyncResult must be a waitable object and its underlying synchronization primitive should be signaled after the call is canceled or completed. The waitable objects support waiting to become signaled with "any" or "all" semantics.
– The Cancel method is a request to cancel processing of the method after the desired timeout period has expired and the server is recommended to honor it. The client is recommended to not destory the resources such as file objects as the server may be actively using them. It’s safe for the client to destory the resources after the IsCompleted or IsCancelled property returns true.
– The semantics of the one-way qualifier: The callback itself may execute asynchronously through the context dispatcher infrastructure.
– Calling EndInvoke before the asynchronous operation is complete will block the caller. Calling it a second time with the same AsyncResult is undefined.

class FactorizeCallback {
   public FactorizeCallback(unsigned long number) {
      _ulNumber = number;
   // Note the qualifier one-way. See last paragraph of this section for its explanation.
   public one-way boolean FactorizedResults(IAsyncResult ar) {
      unsigned long factor1, factor2;
      FactorizingDelegate fd = ar.AsyncObject;
      fd.EndInvoke(factor1, factor2, ar);

      // Output the results.
      Console.Writeline("Factors of" + _ulNumber + ":" + factor1 + " " + factor2);
   private unsigned long _ulNumber

// Client code.
delegate FactorizingDelegate(unsigned long factorizableNum,
       ref unsigned long primefactor1,
       ref unsigned long primefactor2);
PrimeFactorizer pf = new PrimeFactorizer();
FactorizingDelegate fd = new FactorizingDelegate(pf.Factorize);

// ———————————————————————————————-
// Async Variation 1.
unsigned long factorizableNum = 1000589023, temp;

// Define the asynccallback delegate.
FactorizeCallback fc = new FactorizeCallback(factorizableNum);
AsyncCallback cb = new AsyncCallback(fc.FactorizedResults);

// Asynchronously invoke the Factorize method on pf.
// Note: If we have pure out parameters, we do not need temp
fd.BeginInvoke(factorizableNum, temp, temp, cb);

// Proceed to do other work.

// ———————————————————————————————-
// Async Variation 2.
// Asynchronously invoke the Factorize method on pf.
// Note: If we have pure out parameters, we do not need temp
unsigned long factorizableNum = 1000589023, temp, factor1, factor2;
IAsyncResult ar = fd.BeginInvoke(factorizableNum, temp, temp, NULL);

// Do other work.

// Wait for the callback.

// Output the results.
fd.EndInvoke(factor1, factor2, ar);
Console.Writeline("Factors of" + factorizableNum + ":" + factor1 + " " + factor2);

– Supply or DO NOT supply the callback delegate when beginning an asynchronous operation.
– Poll the returned IAsyncResult waitable object for completion.
– End the operation prematurely thereby being blocked until the operation completes.
– Wait on the IAsyncResult waitable object using a timeout to wake up periodically.
– End the operation in the callback routine.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s