How to allow thread to finish before next iteratation/check using a Timer object?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stubbulon5
    New Member
    • Apr 2010
    • 2

    How to allow thread to finish before next iteratation/check using a Timer object?

    Hi there,

    Im using a Timer with an interval of 1 min (60000 milliseconds) to start a thread in the Global.cs of my application. Everything works fine until the the thread job takes more than 1 min to complete. When the job takes longer that 1 min, the old (currently running) job never seems to get to completion.

    Basically I only want to re-run the thread once its complete but using the Timer so I can potentially leave gaps inbetween each Thread jobcall.

    ANY help would be appreciated!

    I list the code in my Global.cs file below:

    Code:
          protected void Application_Start(Object sender, EventArgs e) {
             System.Timers.Timer aTimer = new System.Timers.Timer();
             aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnPopulateDatabaseFromDynamicDataSource);
             aTimer.Interval = 60000; // Every min
             aTimer.Enabled = true;
          }
    
    
    
          
          private static void OnPopulateDatabaseFromDynamicDataSource(object source, System.Timers.ElapsedEventArgs e) {
    
             try {
    
                System.Threading.Thread objWorkerThread
                   = new System.Threading.Thread(HelperClass.PopulateDBFromDynaDataSource);
    
                objWorkerThread.Name = "DynaPopulate";
    
                objWorkerThread.IsBackground =
                   true;
    
                objWorkerThread.Start();
             }
             catch (Exception ex) {
    
                // Do nothing for now!!
             }
    
          }
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Hi!

    Could you post some parts (you don't have to show the business-logic if you don't want to) of HelperClass.Pop ulateDBFromDyna DataSource-method?

    The code you've provided looks quite well to me, so I think the problem could be in that method.
    You could also do some debug-print on the beginning of PopulateDB...()-method to see if it's called every 60 seconds, independent if the last call was finished within this 60 seconds.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Instead of hoping that the job completes in 1 minute, and basing everything on a timer...

      What about raising and event when the processing is completed, then doing whatever it is you need to do next - even if it is to start the next job? That way if the job completes in 10 seconds you don't waste 50, and if it takes 2 minutes you don't bail out early.

      Comment

      • Stubbulon5
        New Member
        • Apr 2010
        • 2

        #4
        Thanks both for your answers,

        tlhintoq, thats for your answer. I think you have nailed it. Ill make the class Im calling raise an event and set a public boolean property 'finished' to true. Ill only run the thread again if 'finished' = true.

        Nice one

        Comment

        Working...