Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, May 19, 2012 8:39 AM
I have a WPF application to start a measurement via a backgroundworker. Within the DoWork() method i subscribed events which indicate me the status of the measurement and allow to stop the measurement (CancelAsync) if necessary (handeled in the progress changed eventhandler). This works fine for the first time. But when I restart the measurement the following error appears (this is the second time RunWorkerAsync() is called):
"This operation has already had OperationCompleted called on it and further calls are illegal." (refers to the backgroundWorkerMeasurement.ReportProgress call)
Has somebody had similar problems and has a solution for it? Isn't it possible to subscribe events in the DoWork() handler? Are there other possibilities to solve my requirements?
Thanks for all help and support in advance!
The code of the DoWork() method is as follow:
void backgroundWorkerMeasurement_DoWork(object sender, DoWorkEventArgs e)
{
try
{
if (DeviceStatus== DeviceStatus.DEV_STAT_READY)
{
backgroundWorkerMeasurement.ReportProgress(10, LocalizedResourceManager.GetString("MeasurementRunning"));
device.OnErrorChanged += (o, a) =>
{
backgroundWorkerMeasurement.ReportProgress(0, a);
};
device.OnMeasInfoChanged += (o, a) =>
{
backgroundWorkerMeasurement.ReportProgress(0, a.eMeasInfo);
};
device.OnTestFinished += (o, a) =>
{
backgroundWorkerMeasurement.ReportProgress(0, LocalizedResourceManager.GetString("MeasurementFinished"));
backgroundWorkerMeasurement.CancelAsync();
};
device.RemoteStart(DeviceData);
while (!backgroundWorkerMeasurement.CancellationPending)
{
Thread.Sleep(100);
}
if (backgroundWorkerMeasurement.CancellationPending)
{
device.RemoteDeactivate();
device.Disconnect();
device.Dispose();
e.Cancel = true;
}
backgroundWorkerMeasurement.DoWork -= backgroundWorkerMeasurement_DoWork;
e.Result = true;
}
else
{
IsMeasurementRunning = false;
UserInteractionProvider.ShowDialog("uiaDialogHeaderMeasurement", "uiaNotReadyForMeasurement", DialogIcon.Warning);
}
}
catch
{
e.Result = false;
}
}
All replies (3)
Saturday, May 19, 2012 3:52 PM âś…Answered
Dispose of your worker instance and use a new one every time you wish to re-use the BGW. Any given BGW instance is associated with a particular thread, a thread that comes from the ThreadPool. After the Completed event is fired, the worker's thread is returned to the ThreadPool. It is possible for something else to need a ThreadPool thread and wind up grabbing the one associated with your worker instance. It wasn't being used, so why not?
When you try to re-use your worker instance by calling RunWorkerAsync the thread is not available, and it appears an assumption is made that when the worker's thread is busy it always means that the worker instance has not completed its' current task. There are scenarios where that is not always the case. You can avoid this type of scenario by always grabbing a new worker instance, and always disposing of the old one.
Hope this helps.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
Saturday, May 19, 2012 9:28 AM | 1 vote
It is certainly possible to subscribe to events in DoWork. What's not possible is calling ReportProgress after the background worker has completed and it looks like the device raises an event after worker completed. I don't know the exact behavior of your "device" but you should probably remove those events before completing or cancelling the background worker.
That said, your use of BackgroundWorker is rather strange. It looks like your device operates asynchronously anyway, that's why it has Start and events. What's the point of background worker here?
Saturday, May 19, 2012 9:59 AM
Hi Mike,
thanks a lot for your answer - I will try to solve the issue without a backgroundworker and inform you if this will solve the problem. At the moment I don't no wether the measurement works asynchronously or not.