I'm currently writing an application that will control positioning of a measurement device. Because of the hardware involved I need to poll for the current position value constantly while running the electric motor. I'm trying to build the class responsible for this so that it does the polling on a background thread and will raise an event when the desired position is reached. The idea being that the polling won't block the rest of the application or the GUI. I wanted to use the new Threading.Task.Task class to handle all the background thread plumbing for me.
I haven't got the hardware yet, but have build a test stub to simulate this behavior. But when I run the application like this the GUI still blocks. See a simplified example of the code below (not complete and not using separate class for device control). The code has a sequence of measurement steps, the application has to position and then measure for each step.
public partial class MeasurementForm: Form
{
private MeasurementStepsGenerator msg = new MeasurementsStepGenerator();
private IEnumerator<MeasurementStep> steps;
// actually through events from device control class
private void MeasurementStarted()
{
// update GUI
}
// actually through events from device control class
private void MeasurementFinished()
{
// store measurement data
// update GUI
BeginNextMeasurementStep();
}
private void MeasurementForm_Shown(object sender, EventArgs e)
{
steps = msg.GenerateSteps().GetEnumerator();
BeginNextMeasurementStep();
}
...
...
private void BeginNextMeasurementStep()
{
steps.MoveNext();
if (steps.Current != null)
{
MeasurementStarted();
MeasureAtPosition(steps.Current.Position);
}
else
{
// finished, update GUI
}
}
// stub method for device cont