Accessing asynchronous algorithms
All algorithms derived from ILAsyncAlgorithm must implement individual constructors with typed parameter list.
The list varies from algorithm to algorithm. All parameters needed for computations by the algorithm will
be supplied as those constructor parameters. An algorithm may also implements an individual result structure holding
all results of the algorithm. The result struct is accessible through the Result property implemented by all
ILAsyncAlgorithm derived classes.
Flow control
Asynchronous algorithms provide the following functions for flow control which may be called once an algorithm instance exists:
| Method | Purpose |
|---|---|
RunAsync() | Start the calculation in a new thread. This method is called in order to start the algorithm calculation. |
Suspend() | Freeze the execution of the algorithm at its currents state. This method will halt the execution of the algorithm until one of the methods Resume, Cancel or Kill was called.
|
Resume() | Resume the execution of a suspended algorithm |
Cancel() | Stop the algorithm by a clean exit. Calling this method is the recommended way to end the algorithms execution. You should alway call this method first. This will give the algorithm the chance to make a clean exit and provide a valid (intermediate) result. |
Kill() | Immediately kill the execution of the calculation worker thread. This should only be called, if the Cancel method did not work or did not work fast enough. Killing an algorithm this way will immediately kill the thread. The algorithm therefore does not get the chance to do any cleaning up and/or cannot provide a valid result.
|
State information & properties
The current state and progress of the algorithm may always be queried from the properties State and
Progress. Therefore following ILAlgorithmState state enumeration values exist:
| State | Description |
|---|---|
| Initialized | The initial state each newly created ILAsyncAlgorithm instance gets assigned to |
| Running | The algorithm is running normally. |
| Suspended | The algorithm was freezed, waiting to get resumed. |
| Finished | The algorithm finished its calculation successfully. The result has been provided. |
| Canceled | The algorithm has been stopped or killed. A result may be available (depends on the algorithm implementation). |
Eventing model
ILAsyncAlgorithms use events to inform registered delegates about state, progress
and user defined data. Those events may be fired before, while and after execution of the calculations. In
difference to
the synchronous ILAlgorithm class, the event model is only used for one way information, i.e. the 'Cancel' member of the event
arguments is ignored on return from the event handler.
The following events can get fired from instances of ILAsyncAlgorithm:
| Event | ... is thrown on |
|---|---|
| StateChanged | state changes (start, cancel, suspend, resume, kill) |
| ProgressChanged | progress changes |
The next section will present a complete asynchronous application example using the SimpleAsyncSample class, implemented in ILNumerics.Net. It demonstrates the use of asynchronous algorithms in general.