Answered Timer or ThreadPoolTimer?

Toate mesajele

  • 11 aprilie 2012 01:45
    Moderator
     
     Răspuns Are cod

    Create different timers with different handlers for different uses.

    As you note, there is no way to pass state with the timer. The TimerElapsedHandler's only argument is the ThreadPoolTimer it is handling.

    One possibility would be to use a TimerElapsedHandler which passes your state object to a worker function. Something like the following (untested)

     object stateObject = this;
     ThreadPoolTimer timer = ThreadPoolTimer.CreatePeriodicTimer((t) => { TimerHandlerWorker(t, stateObject); }, new TimeSpan(1000));
               

    --Rob