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.
.NET ProgrammingProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Scott McDaniel (EE MVE )

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kyle Abrahams

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SStory

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.
Scott McDaniel (EE MVE )

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Scott McDaniel (EE MVE )

ASKER
Thanks for the information!