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 control (actually in seperate class)
public void MeasureAtPosition(decimal position)
{
// simulate polling
var context = TaskScheduler.FromCurrentSynchronizationContext();
Task task = Task.Factory.StartNew(() =>
{
Thread.Sleep(sleepTime);
}, TaskCreationOptions.LongRunning)
.ContinueWith(_ =>
{
MeasurementFinished();
}, context);
}
}
I would expect the Task to run the Thread.Sleep command on a background thread so control returns to the main thread immediately and the GUI doesn’t get blocked. But the GUI still gets blocked. It’s like the Task runs on the main thread. Any ideas on what I’m doing wrong here?
Thanks
Because your continuation task (via
ContinueWith) specifies aTaskSchedulerthe TPL uses that for all other tasks kicked off further down the call stack regardless of whether you actually specified it. In other words, calls toTask.Factory.StartNeworiginating from theActiondelegate specified inContinueWithwill automatically use the specifiedTaskSchedulerby default.I have modified your code to help you better visualize what is going on.
I examined the code in
ContinueWithvia Reflector and I can confirm that it is attempting to discover the execution context used by the caller. Yes, believe it or not, and despite your natural intuition to the contrary, that is exactly what it is doing.A better solution would probably be to have a dedicated thread to do the hardware polling.