IntentCombine the Pollable and Notifying Thread Managers into one pattern that allows the client to use the best of both worlds . ProblemSometimes 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. ForcesUse the MultiSync Thread Manager pattern when:
StructureMultiSync 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.
ConsequencesThe MultiSync Thread Manager has the following benefits and liabilities:
Participants
ImplementationThe 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
|