none
timer not working in backgroundworker RRS feed

  • Question

  • i'm running a background worker and I have a timer that does all the work(supposed to) but the .tick never gets executed.  the DoWork calls the function that does this

    tmrBuildDataTree.Interval = 5;
    tmrBuildDataTree.Tick += TmrBuildDataTree_Tick;
    tmrBuildDataTree.Enabled = true;
    
    

    I can step through it and see this happen but the .tick function never kicks in...

    I imagine it has something to do with the fact that its a backgroundworker... but dunno.

    BadButBit


    my code is perfect until i don't find a bug

    Friday, February 15, 2019 11:59 PM

Answers

  • Use a StopWatch, you can control its start stop and interval with greater accuracy than a Timer and you can do that all within the DoWork eventhandler.
    • Marked as answer by Christ Kennedy Sunday, February 17, 2019 8:58 AM
    Saturday, February 16, 2019 8:23 PM
  • If you want to start the timer from BackgroundWorker, then try this:

       BeginInvoke( new Action( () =>

       {

          tmrBuildDataTree.Interval = 5;

          tmrBuildDataTree.Tick += TmrBuildDataTree_Tick;

          tmrBuildDataTree.Enabled = true;

       } ) );

     


    • Edited by Viorel_MVP Saturday, February 16, 2019 9:29 PM
    • Marked as answer by Christ Kennedy Sunday, February 17, 2019 8:58 AM
    Saturday, February 16, 2019 9:29 PM

All replies

  • You can pass values out from the background worker using the progress changed event handler

     private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
            {
                 backgroundWorker1.ReportProgress(5, "true"); 
            }
    
            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                tmrBuildDataTree.Interval = e.ProgressPercentage;
                if (e.UserState.ToString() == "true") { tmrBuildDataTree.Enabled = true; }
            }

    Saturday, February 16, 2019 3:54 PM
  • the background worker can't tell its own timer to stop and go?  if the backgroundworker can paint the objects it created why can't it just enable/disable the timer it created?  do i really have to have an outside timer that has to go through the intermediary of a progressChange event to enable/disable its own timer?

    BadButBit


    my code is perfect until i don't find a bug

    Saturday, February 16, 2019 4:42 PM
  • Use a StopWatch, you can control its start stop and interval with greater accuracy than a Timer and you can do that all within the DoWork eventhandler.
    • Marked as answer by Christ Kennedy Sunday, February 17, 2019 8:58 AM
    Saturday, February 16, 2019 8:23 PM
  • If you want to start the timer from BackgroundWorker, then try this:

       BeginInvoke( new Action( () =>

       {

          tmrBuildDataTree.Interval = 5;

          tmrBuildDataTree.Tick += TmrBuildDataTree_Tick;

          tmrBuildDataTree.Enabled = true;

       } ) );

     


    • Edited by Viorel_MVP Saturday, February 16, 2019 9:29 PM
    • Marked as answer by Christ Kennedy Sunday, February 17, 2019 8:58 AM
    Saturday, February 16, 2019 9:29 PM