C# Class ANHAdmin.AsyncOperation

This base class is designed to be used by lengthy operations that wish to support cancellation. It also allows those operations to invoke delegates on the UI Thread of a hosting control.
This class is from the MSDN article: http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/default.aspx (C) 2001-2002 I D Griffiths Please see the article for a complete description of the intentions and operation of this class.
Show file Open project: swganhtools/anhserverconfigurationtool

Public Methods

Method Description
AsyncOperation ( ISynchronizeInvoke target ) : System

Initialises an AsyncOperation with an association to the supplied ISynchronizeInvoke. All events raised from this object will be delivered via this target. (This might be a Control object, so events would be delivered to that Control's UI thread.)

Cancel ( ) : void

Attempt to cancel the current operation. This returns immediately to the caller. No guarantee is made as to whether the operation will be successfully cancelled. All that can be known is that at some point, one of the three events Completed, Cancelled, or Failed will be raised at some point.

CancelAndWait ( ) : bool

Attempt to cancel the current operation and block until either the cancellation succeeds or the operation completes.

Start ( ) : void

Launch the operation on a worker thread. This method will return immediately, and the operation will start asynchronously on a worker thread.

WaitUntilDone ( ) : bool

Blocks until the operation has either run to completion, or has been successfully cancelled, or has failed with an internal exception.

Protected Methods

Method Description
AcknowledgeCancel ( ) : void

This is called by the operation when it wants to indicate that it saw the cancellation request and honoured it.

DoWork ( ) : void

To be overridden by the deriving class - this is where the work will be done. The base class calls this method on a worker thread when the Start method is called.

FireAsync ( Delegate dlg ) : void

Utility function for firing an event through the target. It uses C#'s variable length parameter list support to build the parameter list. This functions presumes that the caller holds the object lock. (This is because the event list is typically modified on the UI thread, but events are usually raised on the worker thread.)

Private Methods

Method Description
CompleteOperation ( ) : void
FailOperation ( Exception e ) : void

InternalStart ( ) : void

Method Details

AcknowledgeCancel() protected method

This is called by the operation when it wants to indicate that it saw the cancellation request and honoured it.
protected AcknowledgeCancel ( ) : void
return void

AsyncOperation() public method

Initialises an AsyncOperation with an association to the supplied ISynchronizeInvoke. All events raised from this object will be delivered via this target. (This might be a Control object, so events would be delivered to that Control's UI thread.)
public AsyncOperation ( ISynchronizeInvoke target ) : System
target ISynchronizeInvoke An object implementing the /// ISynchronizeInvoke interface. All events will be delivered /// through this target, ensuring that they are delivered to the /// correct thread.
return System

Cancel() public method

Attempt to cancel the current operation. This returns immediately to the caller. No guarantee is made as to whether the operation will be successfully cancelled. All that can be known is that at some point, one of the three events Completed, Cancelled, or Failed will be raised at some point.
public Cancel ( ) : void
return void

CancelAndWait() public method

Attempt to cancel the current operation and block until either the cancellation succeeds or the operation completes.
public CancelAndWait ( ) : bool
return bool

DoWork() protected abstract method

To be overridden by the deriving class - this is where the work will be done. The base class calls this method on a worker thread when the Start method is called.
protected abstract DoWork ( ) : void
return void

FireAsync() protected method

Utility function for firing an event through the target. It uses C#'s variable length parameter list support to build the parameter list. This functions presumes that the caller holds the object lock. (This is because the event list is typically modified on the UI thread, but events are usually raised on the worker thread.)
protected FireAsync ( Delegate dlg ) : void
dlg System.Delegate
return void

Start() public method

Launch the operation on a worker thread. This method will return immediately, and the operation will start asynchronously on a worker thread.
public Start ( ) : void
return void

WaitUntilDone() public method

Blocks until the operation has either run to completion, or has been successfully cancelled, or has failed with an internal exception.
public WaitUntilDone ( ) : bool
return bool