none
Task after timer not restarting timer RRS feed

  • Question

  • Hello,

    I got this problem I can't seen to work it out

    I have a class with a timer that executes a method on timer_Tick event

    private void _timer_Tick(object sender, EventArgs e) { if (onTick != null) { onTick(this, new argumentoEvento("Timer finished", _nomeServico)); }

    this._timer.Stop(); ExecutaWebServiceDinamico(true); }


    on the method

    ExecutaWebServiceDinamico

     I have something like this

    private void ExecutaWebServiceDinamico(bool ativaTimer) {            
                Task t =
                        Task.Factory.StartNew(() => ExecutaWebServiceDinamico(_parametrosChamada)).ContinueWith(ant => Start(ativaTimer, ref this._timer));            
            }

    and on the Start delegate

    private void Start(bool ativaTimer, ref Timer t) {
                if (ativaTimer) {
                    t.Enabled = true;
                    Start();
                }
            }

    the thing is the timer won't restart itself

    Did I make myself clear?

    what is the problem about starting a task and when it finishes restarting the timer? I'm I loosing something here, does ContinueWith gets called on a diferent thread so it gets killed when the task finishes?

    If some1 can help me out

    TYVM

    Thursday, August 9, 2012 6:44 PM

Answers

  • If this is a Windows Forms timer, you may need to marshal the call to continue back onto the UI thread:

    This could be done via:

                Task t =
                        Task.Factory.StartNew(() => ExecutaWebServiceDinamico(_parametrosChamada)).ContinueWith(ant => Start(ativaTimer, ref this._timer), TaskScheduler.FromCurrentSynchronizationContext());  


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by mapelli0 Thursday, August 9, 2012 7:15 PM
    Thursday, August 9, 2012 7:08 PM
    Moderator

All replies

  • If this is a Windows Forms timer, you may need to marshal the call to continue back onto the UI thread:

    This could be done via:

                Task t =
                        Task.Factory.StartNew(() => ExecutaWebServiceDinamico(_parametrosChamada)).ContinueWith(ant => Start(ativaTimer, ref this._timer), TaskScheduler.FromCurrentSynchronizationContext());  


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by mapelli0 Thursday, August 9, 2012 7:15 PM
    Thursday, August 9, 2012 7:08 PM
    Moderator
  • OMG tyvm

    that did the trick

    TY very very very much

    Thursday, August 9, 2012 7:15 PM