Sample asynchronous algorithms implementation
In the following example the complete
SimpleAsyncSample class is displayed. We have used this class in the previous section. It demonstrates the
implementation of an user defined algorithm including a user defined parameter struct and all nesseccary event handling.
// class SimpleAsyncSample.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ILNumerics;
namespace ILNumerics.Algorithms {
public class SimpleAsyncSample : ILAsyncAlgorithm {
// define sample input struct
private struct InputParamStruct {
public double parameter1;
public int iterationCount;
public ILArray<double> parameter3;
}
// define sample output struct
public struct SimpleAsyncSampleResult {
public string message;
public ILArray<double> result;
}
// hold the imput parameter
private InputParamStruct m_inputParameter;
// property to cast the underlying 'object' result
// back to specific result struct
public SimpleAsyncSampleResult Result {
get {
object res = base.GetResult();
return (SimpleAsyncSampleResult)res;
}
}
// Implement a constructor receiving all parameter nesseccary
// for your specific algorithm here.
// Do not forget to call the base class here!
public SimpleAsyncSample(Control control,
double param1,
int itCounter,
ILArray<double> param3)
: base (control) {
m_inputParameter.parameter1 = param1;
m_inputParameter.iterationCount = itCounter;
m_inputParameter.parameter3 = param3;
}
// the actual calculation - here it's for time consumer only
protected override object AlgorithmFunction(object parameter) {
InputParamStruct input = (InputParamStruct)parameter;
SimpleAsyncSampleResult ret;
ret.message = "The algorithm has finished!";
ret.result = randn(100,10,200);
for (int i = 0; i < input.iterationCount ; i++) {
// simulate delay for 1 sec with an inner loop
for (int t = 0; t < 10; t ++) {
if (CancelPending)
return -1.0;
SetProgress((double)i / (double)input.iterationCount,"calculating...");
System.Threading.Thread.Sleep(100);
}
}
return ret;
}
// provides the parameter struct to the algorithm function
protected override object CompileParameter() {
return m_inputParameter;
}
}
}