MultiSync Thread Manager


Intent

Combine the Pollable and Notifying Thread Managers into one pattern that allows the client to use the best of both worlds .

Problem

Sometimes you need it all. Sometimes you not only want to be notified of completion of the operation but also want to be able periodically to check the status of the operation. In addition, sometimes you just do not know how the client will prefer to call your operation. In either situation, combining the Pollable Thread Manager and the Notifying Thread Manager gives the best of both worlds.

In processing a credit card transaction, for example, there are very distinct steps to be processed : verifying card information, contacting bank, etc. The application obviously has to know when the transaction is complete, but the ability periodically to poll the "processor" to know the status during the process helps the user to know something is going on. The application could request to be notified upon completion of the transaction but periodically update the user as to its status.

In developing a component for someone else to use, either as third-party software or even for other developers to use within the same organization, leaving the caller the option of how to call your operation greatly increases the usability of your components . The caller can choose to call your component periodically for status updates or may just fire off a call and do something else until notified of completion, based on what works best for their user interface.

Forces

Use the MultiSync Thread Manager pattern when:

  • Client should have the ability to work in a Notifying mode, Pollable mode, or both.

Structure

MultiSync Thread Manager pattern (Figure 3.7) is simply a combination of the two previous patterns. The client can be any class but must be a Windows Control to use the event Notifying approach to execution. Execution can be initiated on the MultiSync Thread Manager either via the ExecuteAsync method, which requires you to provide the delegates to use for notification, or the BeginExecution method. The client can periodically poll the current execution status through the WaitFor method or just wait for notification. As in the Pollable Thread Manager, we use an enumeration to provide a more complete return code.

Figure 3.7. MultiSync Thread Manager structure.

graphics/03fig07.gif

Consequences

The MultiSync Thread Manager has the following benefits and liabilities:

  1. It provides options when calling asynchronous operations . This pattern is intended to provide options to the client. You will probably notice the absence of the word simple here. Adding options will always add some level of complexity.

  2. It provides the best of both worlds and the negative consequences that may go with them.

Participants

  • Client ” The client can be any class that can periodically check for completion of the process execution but must be derived from Windows.Form.Control in order to use the Notifying method of execution.

  • MultiSync ThreadManager ” Any operation that you want to execute asynchronously but want the flexibility to call either by polling for completion or by waiting to be notified of completion.

  • AsyncReturn ” An enumeration that provides processing status, as well as completion success.

Implementation

The implementation for the Notifying Thread Manager and Pollable Thread Manager intentionally allows for an easy overlap of the two patterns. The only change you must make from the previous two patterns will exist in the protected DoExecution method.

Starting from the DoExecution method of the Notifying Thread Manager, additional if statements are added to ensure that the control is not null before attempting to do the notification.

Listing 3.10 MultiSync Thread Manager DoExecution method.
 protected void DoExecution() {    object[] paramArray = new object[3];    try    {       // Get the answer       mstrFactor = Factorer.Factor(mToFactor);       // If we have a success delegate defined, call it       if (mControlToNotify != null)       {          // Build the parameter array for the completion event          paramArray[0] = mID;          paramArray[1] = mToFactor;          paramArray[2] = strFactor;          // Invoke the "Success" delegate    mControlToNotify.BeginInvoke(mFactoringComplete,             paramArray);       }    }    catch(Exception ex)    {       .       .       .    } } 

Related Patterns

  • NotifyingThreadManager (Eshelman)

  • PollableThreadManager (Eshelman)



.NET Patterns. Architecture, Design, and Process
.NET Patterns: Architecture, Design, and Process
ISBN: 0321130022
EAN: 2147483647
Year: 2003
Pages: 70

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net