C# WP7 - Disbatch Timer, How To Pause And Resume?

rover2341

Is riding a roller coaster...Wee!
Reaction score
113
C# Dispatch Timer. (WP7).

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx
It claims I can change the .IsEnabled get and set. BUT I CANT. in WP7 its only read only.

My Requirement: Pause, Resume. How to get it to work....

Looking for suggestions or insight on how to deal with this.


Creating the ability to do pause and resume manually is not perferd.
- I know its possible to set up 2 functions I make up.....startTimer, StopTimer. That will find the amount of time the timer has been going, and how much is left. then stop the timer, and set the timer to total time to whats left, so when it starts again it will finsh on time.

But there are 2 issues with this.
1. I have to create 2 functions and some varbiles for each timer I have to pause (I have 4 of them).
2. Having hard time getting this to work correctly, but should.

Other Timer Types
//thearding.timer
//stopwatch

But they dont seem to solve the problem. I read online, that this timer should be able to pause and resume just fine but I cant figure out how.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Apparently you have to create this functionality manually.
At least that's what I got from http://www.codeproject.com/Answers/252810/How-to-pause-resume-the-timer-in-WPF#answer1 and http://stackoverflow.com/questions/11427532/how-to-pause-a-dispatchtimer .


But you don't have to tediously create a bunch of variables for each timer. I've never done WP code nor have I much experience in C# but with inheritance I whipped up something:

Code:
public class CustomTimer : DispatcherTimer{
    private DateTime PauseTime;
    private TimeSpan FullTickInterval;
   
    //one-time event handler
    private void ResetIntervalOnNextTick(object sender, EventArgs e){
        CustomTimer ct = ((CustomTimer)sender);
        ct.Tick -= new EventHandler(ResetIntervalOnNextTick);
        ct.Interval = FullTickInterval;
        //ct.Start(); <- necessary?
    }
 
    public void Pause(){
        PauseTime = DateTime.Now;
        FullTickInterval = this.Interval;
        this.Stop();
    }
 
    public void Resume(){
        //Get time difference between now and when the timer was paused
        TimeSpan t = DateTime.Now - PauseTime;
        //Find out how long until the next timer tick should occur
        TimeSpan partialTick = FromMilliseconds(FullTickInterval.Milliseconds - (t.Milliseconds % FullTickInterval.Milliseconds));
       
        //Add a one-time event handler that'll reset the interval on the next tick
        this.Tick += new EventHandler(ResetIntervalOnNextTick);
        this.Interval = partialTick;
       
        this.Start();
    }
}

That code hasn't left the dry dock (= I didn't try it) and at the very least it'll bug out if you change the timer interval while the timer is paused.
But I'd imagine it'll work like this.
 

rover2341

Is riding a roller coaster...Wee!
Reaction score
113
Thanks For the Help! With that, I was able to get it up and running, in a few min.

Solved
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top