Example of using Timer and BackgroundWorker together

Posted on 2008-11-13
Last Modified: 2012-05-05
I have an app that isn't working correctly. I have intervals that I want the BackgroundWorkers to run but they are not "sleeping" for the full interval.

I have three BackgroundWorkers (dropped on the form) that I need to run at specified intervals.

    Dim INTERVAL_ProcessMessages As Integer = 10000
    Dim INTERVAL_PatientRequests As Integer = 180000
    Dim INTERVAL_VisitRequests As Integer = 180000

We call the Sleep method of the Thread in each of the DoWork events like this:

But, we are logging the work done in the DoWork and have found that the bwVisitRequests_DoWork is running
every 3-15 seconds, here is a copy of our log showing the VisitRequests:
Message_Type  Message_ID                    Request_Time
GETDATEVISITS GETDATEVISITS_20081113061959 11/13/2008 6:19:59 AM
GETDATEVISITS GETDATEVISITS_20081113062005 11/13/2008 6:20:05 AM
GETDATEVISITS GETDATEVISITS_20081113062028 11/13/2008 6:20:28 AM
GETDATEVISITS GETDATEVISITS_20081113062030 11/13/2008 6:20:30 AM
GETDATEVISITS GETDATEVISITS_20081113062033 11/13/2008 6:20:33 AM
GETDATEVISITS GETDATEVISITS_20081113062114 11/13/2008 6:21:14 AM
GETDATEVISITS GETDATEVISITS_20081113062116 11/13/2008 6:21:16 AM
GETDATEVISITS GETDATEVISITS_20081113062120 11/13/2008 6:21:20 AM
GETDATEVISITS GETDATEVISITS_20081113062134 11/13/2008 6:21:34 AM
GETDATEVISITS GETDATEVISITS_20081113062152 11/13/2008 6:21:52 AM
GETDATEVISITS GETDATEVISITS_20081113062159 11/13/2008 6:21:59 AM
GETDATEVISITS GETDATEVISITS_20081113062222 11/13/2008 6:22:22 AM
GETDATEVISITS GETDATEVISITS_20081113062247 11/13/2008 6:22:47 AM
GETDATEVISITS GETDATEVISITS_20081113062309 11/13/2008 6:23:09 AM
GETDATEVISITS GETDATEVISITS_20081113062327 11/13/2008 6:23:27 AM
GETDATEVISITS GETDATEVISITS_20081113062332 11/13/2008 6:23:32 AM
GETDATEVISITS GETDATEVISITS_20081113062338 11/13/2008 6:23:38 AM

We want to use Timers to handle the intervals but still use the BackgroundWorkers to do the work without
doing too much to modify the code.

Any ideas on how to do this?
Question by:EddieShipman
    LVL 15

    Accepted Solution

    You do understand that timers are not threads.  You are still working with a logic that moves step by step in a single thread.  A timer will just interupt your logic flow and do stuff then return back to the original logic flow.  The fact that you are using several timers hints at an assupption of multi-thread logic (I may be mistaken..if so sorry).  But at any rate, it is important to keep threading principals strait in your head when working with timers.
    LVL 26

    Author Comment

    Actually, we've found the problem and are correcting it now.
    Thanks for taking a look at this.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now