Asynchronous task constructorsΒΆ
PeriodicTask(action, periodMilliseconds, excutingNode) constructor
Create a PeriodicTask
task that executes code at regular time intervals.
PeriodicTask(Action action, int periodMilliseconds, IUANode executingNode);
Arguments
action
Action
Method or lambda expression to execute.
periodMilliseconds
int
Period of time between one execution and the next.
executingNode
IUANode
Node in which to execute the code.
Example
Below is an example where a myPeriodicTask
task is created that executes the IncrementVariable()
method every second (1000 milliseconds). The method increments the value of the variable1
variable by one unit each time it is executed.
public override void Start()
{
myPeriodicTask = new PeriodicTask(IncrementVariable, 1000, LogicObject);
myPeriodicTask.Start();
}
public override void Stop()
{
myPeriodicTask.Dispose();
}
private void IncrementVariable()
{
variable1.Value = variable1.Value + 1;
}
private PeriodicTask myPeriodicTask;
Note
in this example, the IncrementVariable()
method has no PeriodicTask
argument as the simplicity of its code does not require the task to be canceled.
DelayedTask(action, delayMilliseconds, executingNode) constructor
Creates a DelayedTask
task that executes code after a desired time interval.
DelayedTask(Action action, int delayMilliseconds, IUANode executingNode);
Arguments
action
Action
Method or lambda expression to execute.
delayMilliseconds
int
Time after which the method or lambda expression is executed.
executingNode
IUANode
Node in which to execute the code.
Example
Below is an example where a myDelayedTask
task is created to execute the ResetLabelText()
method after 10 seconds (10000 milliseconds). The method cancels the text of a label1
label.
public override void Start()
{
myDelayedTask = new DelayedTask(ResetLabelText, 10000, LogicObject);
myDelayedTask.Start();
}
public override void Stop()
{
myDelayedTask.Dispose();
}
private void ResetLabelText()
{
label1.Text = string.Empty;
}
private DelayedTask myDelayedTask;
Note
in this example, the ResetLabelText()
method has no DelayedTask
argument as the simplicity of its code does not require the task to be canceled.
LongRunningTask(action, executingNode) constructor
Creates a LongRunningTask
task to execute time- or CPU-binding codes.
LongRunningTask(Action action, IUANode executingNode);
Arguments
action
Action
Method or lambda expression to execute.
executingNode
IUANode
Node in which to execute the code.
Example
Below is an example in which a myLongRunningTask
task is created to process a CSV file using the ProcessCsvFile()
method. This method has the task itself as argument, the status of which is checked using the IsCancellationRequested
property after reading each row of the CSV file. In this way, the task can be canceled.
using namespace System.IO; // For using the StreamReader
class public override void Start()
{
myLongRunningTask = new LongRunningTask(ProcessCSVFile, LogicObject);
myLongRunningTask.Start();
}
public override void Stop()
{
myLongRunningTask.Dispose();
}
private void ProcessCsvFile(LongRunningTask task)
{
using (var reader = new StreamReader(pathToCsvFile))
{
while (!reader.EndOfStream)
{
// Check whether task cancellation has been requested
if (task.IsCancellationRequested)
{
// Properly handle task cancellation here
return;
}
string line = reader.ReadLine();
// Process line
}
}
}
private LongRunningTask myLongRunningTask;