Link to home
Start Free TrialLog in
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )Flag for United States of America

asked on

.NET - Using the BackGroundWorker to handle multiple tasks

I'm using VB.NET, and I'm currently using two Timer objects - one to monitor the external operation, and a second to perform the db writes.

I've been advised to look into using the BackgroundWorker object instead, and wanted to better understand exactly what this does, and how I'd benefit from using it. I'm hoping for some insight on how the BGW object works, and why it would be better than a Timer object.

Essentially my program interacts with an automated "picking" system. My program writes rows to an integration table. The pick system reads data from those integration tables, and then performs tasks. The pick system reports success by removingthen removes rows from those tables, or writes values into an Error table, and my program responds to those actions. The process boils down to this:

1. My program adds rows to a Detail and Header table (the "integration" tables)
2. "Pick system" reads those rows and performs tasks.
3. "Pick system" removes the rows from those tables upon a successful "pick", or writes records to an Error table upon failure
4. Timer1 in my program queries those tables periodically, and adds values to a Queue object for successful picks.
5. Timer2 reads the Queue objects, and writes values to another database based on the Queue object.

Step 4 is the first Timer. As mentioned above, it checks the integration tables. If there is a successful pick, I add items to a queue.

Step 5 is the second Timer. It reads the next Queue item and attempts to write records to a database. This operation can take some time, depending on the complexity of the data.

My question is - what's the benefit in using the BackGroundWorker instead of the Timer? The Timer object seems ... klunky ... whereas the BGW object seems better suited for what I'm doing - but I don't really understand the BGW and how it works.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott McDaniel (EE MVE )

ASKER

Thanks for that information - I realized after I wrote it that a Timer and a BackgroundWorker are really different, and that they can be used in conjunction with each other.

My thoughts are to use the Timer to periodically check for the necessary markers to begin my process, and to then use the BackgroundWorker to actually perform the database write. That would help prevent my program from freezing during the (possibly) long-running database operations.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Scott,

These are already good enough explanations.  BackgroundWorker is sort of managed threading in a way.   Provide an object with work to do and get back the results when it is done.
Also instead of Timers, you could use reset events
I like this idea. I'm reviewing your code and will post a new question if needed.
Thanks for the information!