Timer or ThreadPoolTimer?
-
10 aprilie 2012 03:00
hi all,
I know that there is a Class named Timer in windows phone 7, and it contains 4 constructors.[http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx]
And there is a Class named ThreadPoolTimer in windows 8 and it contains 2 methods.[http://msdn.microsoft.com/en-us/library/windows/apps/br230587.aspx#methods]
The timer constructors contains a state parameter which is an object containing information to be used by the callback method. but any of the ThreadPoolTimer's method does not contain the similar parameters.
so how can i reuse the timer in windows 8?
Toate mesajele
-
11 aprilie 2012 01:45Moderator
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
- Propus ca răspuns de Rob CaplanMicrosoft Employee, Moderator 11 aprilie 2012 01:45
- Marcat ca răspuns de Min ZhuMicrosoft Contingent Staff, Moderator 20 aprilie 2012 02:51