Sample ILAsyncAlgorithm derived class (C#)
The following example utilizes the SimpleAsyncSample class, which is part of the ILNumerics.Algorithms
namespace. It demonstrates the minimal implementation of such an algorithm inside a graphical Windows.Forms user interface. The algorithm
may be cancelled, killed, suspended and resumed by use of corresponding buttons of the GUI.
In order to make the sample run, download the source code project at the end of this article. Alternatively you may
create the sample from scratch:
- From inside Microsoft Visual Studio create a new WindowsApplication. Use the default name to create the new project.
- create the following controls:
- 5 buttons: Start,Stop,Kill,Suspend,Resume - each named 'buttonStart', 'buttonStop', 'buttonKill' etc...
- 1 label named 'labelState'
- 1 text box, multiline, named 'textResult'
Here comes an example of how the form might look like now.

- add an assembly reference to the ILNumerics.Net.dll to the project: Choose 'Project->Add reference' in order to choose the ILNumerics.Net.dll onto your filesystem.
- add the following using declarations on top of the file 'Form1.cs' (code view):
using ILNumerics; using ILNumerics.Algorithms; - Set the debug output folder to the ILNumerics.Net binary folder (project properties-> build-> output folder)
- add a private variable for storing the algorithm instance into the Form1 class scope:
private SimpleAsyncSample m_algorithm = null; - in the Form1 constructor create an instance of the simple async sample algorithm and connect both events with some default handlers:
public Form1() { InitializeComponent(); // create sample parameter (no meaning in this example) ILArray<double> arrPar = new ILArray<double>(100,23,13,-2,31.2); m_algorithm = new SimpleAsyncSample(this,2.0,10,arrPar); m_algorithm.StateChanged += new ILAlgorithmStateChangedEventHandler( m_algorithm_StateChanged); m_algorithm.ProgressChanged += new ILAlgorithmStateChangedEventHandler( m_algorithm_ProgressChanged); } - in the 'm_algorithm_ProgressChanged' event handler write the events argument member 'Message' into the text box.
// handle the ProgressChanged event - just print a message void m_algorithm_ProgressChanged(object sender, ILAlgorithmEventArgs e) { textResult.Text = e.Message; } - For the handling of StateChange-events we first implement a little helper routine. It takes the enabled state values
for all the buttons and simply sets those properties:
// helper function for setting the control's enabled state private void setControls(bool start, bool suspend, bool resume, bool stop) { buttonStart.Enabled = start; buttonSuspend.Enabled = suspend; buttonResume.Enabled = resume; buttonStop.Enabled = stop; buttonKill.Enabled = stop; } -
That helper we now use in the event handler, enabling and disabling the buttons according to the state of the algorithm:
// handle the StateChanged event - set controls and display result void m_algorithm_StateChanged(object sender, ILAlgorithmEventArgs e) { labelState.Text = e.State.ToString(); switch (e.State) { case ILAlgorithmState.Canceled: setControls(true,false,false,false); break; case ILAlgorithmState.Finished: setControls(true,false,false,false); textResult.Text = m_algorithm.Result.result.ToString(); break; case ILAlgorithmState.Initialized: setControls(true,false,false,false); break; case ILAlgorithmState.Running: setControls(false,true,false,true); break; case ILAlgorithmState.Suspended: setControls(false,false,true,true); break; } } -
Now we still have to give the buttons their functionality. This is very straightforward, since each button does what its name promise.
Double click on the buttons in der designer window and overwrite the default implementation for the event handler:
// buton click handler: just forward the action to the algorithm private void buttonStart_Click(object sender, EventArgs e) { m_algorithm.RunAsync(); } private void buttonStop_Click(object sender, EventArgs e) { m_algorithm.Cancel(1000); } private void buttonKill_Click(object sender, EventArgs e) { m_algorithm.Kill(1000); } private void buttonResume_Click(object sender, EventArgs e) { m_algorithm.Resume(); } private void buttonSuspend_Click(object sender, EventArgs e) { m_algorithm.Suspend(); }
Now close the application while the algorithm is still running. Ups! - most probably you will get an exception. This is due to the fact, we must take care, the worker algorithm is closed before we unload the destination for its events. This is done by the following code, which handles the Form1.Form_Closing event.
In the project explorer select Form1. Activate the flash sign in the property editor tool window and double click on the "FormClosing" entry. Now overwrite the default implementation of the event handler:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if (m_algorithm.State == ILAlgorithmState.Running
|| m_algorithm.State == ILAlgorithmState.Suspended) {
if (MessageBox.Show("algorithm running! Kill?",
"Title",MessageBoxButtons.YesNo)
== DialogResult.Yes) {
if (!m_algorithm.Kill(2000))
e.Cancel = true;
} else {
e.Cancel = true;
}
}
}
This will test for a running state of the algorithm before exiting the application and let the user choose
to kill the thread or to cancel the exit process. Now our little application runs smoothly without distortion. Download source code for this article:
WindowsApplication1.zip - project
WindowsApplication1.zip - project