Calling BackgroundWorker Several Times

J

JMecc

I am recieving data grom a DAQ card and each time to handle it I call
a BackgroundWorker such that the main thread can keep the UI
responsive. I don't need the UI to reflect changes many times per
second when new data comes in, only as fast as it can (but I do need
the new data to be added onto my data array).

Sometimes the new data coming in event tries to call the
BackgroundWorker while it is still handling the previous data entry
and calcs (& you can't start a BackgroundWorker while it is still
running), so I tried using:

While DataInBkgndWorker.IsBusy
System.Threading.Thread.Sleep(20)
End While
DataInBkgndWorker.RunWorkerAsync(pts)

but this ends up in an infinite loop when IsBusy is true.

I was thinking that while I pause the main thread for a few
milliseconds, the BackgroundWorker thread would finish and then the
main thread could call the BackgroundWorker again a few iterations
later. Instead the app fully freezes, using up 0% processor resources
(does nothing but while loop). How can I call the BackgroundWorkers
back-to-back without overlapping?

Jo
 
A

Armin Zingler

JMecc said:
I am recieving data grom a DAQ card and each time to handle it I call
a BackgroundWorker such that the main thread can keep the UI
responsive. I don't need the UI to reflect changes many times per
second when new data comes in, only as fast as it can (but I do need
the new data to be added onto my data array).

Sometimes the new data coming in event tries to call the
BackgroundWorker while it is still handling the previous data entry
and calcs (& you can't start a BackgroundWorker while it is still
running), so I tried using:

While DataInBkgndWorker.IsBusy
System.Threading.Thread.Sleep(20)
End While
DataInBkgndWorker.RunWorkerAsync(pts)

but this ends up in an infinite loop when IsBusy is true.

I was thinking that while I pause the main thread for a few
milliseconds, the BackgroundWorker thread would finish and then the
main thread could call the BackgroundWorker again a few iterations
later. Instead the app fully freezes, using up 0% processor resources
(does nothing but while loop). How can I call the BackgroundWorkers
back-to-back without overlapping?

You could put the work (each time you currently call RunWorkerAsync) in a
queue and have the BGW process the queue all the time.

But, first question is actually, how do you receive the data from the DAQ
card? Do you handle an event (Addhandler/Withevents)? Does it have to be
handled in a user interface thread (like the winforms timer e.g.)?


Armin
 
J

jp2msft

You should create and set a Mutex so that you can prevent this.

Also, it sounds like you may be calling your routine for data acquisition
incorrectly. Could you post your code for that section?
 
C

Cor Ligthert[MVP]

Hi,

In my idea by using a synclocked queue and empty that every time after your
current SystemThreading.Thread sleep.

Cor
 
J

jmecc0

The DAQ triggers an event every time it has collected n datapoints (I
set this to 10-20, taking 0.1-0.2 s to acquire at 100Hz). Upon
recieving the event notification I ask the DAQ dll for the datapoints
as double(,) and give this array to the BackgroundWorker, which in
turn averages the points and checks certain factors to filter the
data, puts them into a custom datapoint class to be added to the main
array of data taken. Within the RunWorkerCompleted sub I update the
on-screen gauges for the user. I have another BackgroundWorker
picking up the main set of datapoints every second or so to perform
computations and show graphs.

Generally the handling of a new datapoint happens before the next
datapoint is ready so there is no problem, but maybe once out of a few
hundred points, one will take too long due to other factors I guess
and it causes a major problem in that the next BackgroundWorker cannot
be called while one is still running. I had figured that just waiting
for the BackgroundWorker to show false for IsBusy would work but it
doesn't. The queue idea sounds like it would be the right solution
though.

I haven't tried mutex on the BackgroundWorker object but I have tried
System.Threading.Monitor and it doesn't seem to help, I think because
it prevents other threads from taking control of this object, but in
my case it is always the same thread (main one) calling the
BackgroundWorker object so monitor allows several event handling calls
on the same thread to use the same BackgroundWorker object.

Thanks all,
Jo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top