1

I want to write an app to show a countdown Timer. When I exit the application by pressing Home or search buttons timer will stop till I go back to the application. Here is my Code:

 public Page1()
{
 InitializeComponent();       
 DispatcherTimer dispatcherTimer = new DispatcherTimer();
 dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
 dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
 dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
   //Till timer is greater than 0 show timer.
}

How can I use the timer in a way that it keeps counting (running) even when I exited the application?

Bahador Saket
  • 995
  • 1
  • 12
  • 24
  • You should probably read [this question & answer](http://stackoverflow.com/questions/13514064/how-to-run-application-in-background-in-windows-phone), which seem to cover ways of approaching what you're asking for. – Anya Shenanigans Aug 16 '13 at 09:45

2 Answers2

0

Save the time when you started the timer in the ApplicationSettings. When the app resumes (activated or re-launched), check the time diff and either restart the timer from the remaining time or show the timer as stopped.

New Dev
  • 48,427
  • 12
  • 87
  • 129
  • I used this method and it works. What I did was I overrid both OnNavigatedto and OnNavigatedFrom. when I exit the page I save the latest value in OnnavigatedFrom and when I am coming back I will update the time. I will save the time value when I am entering again I will get the current time and CurrentTime - PreviousTime will give me the time that I was not in that page. – Bahador Saket Aug 17 '13 at 09:59
-3

Have a WCF or Web API service embedded within a windows service (windows service timer example), With this WCF / Web API service you can control the behaviour without restarting the service.

Community
  • 1
  • 1
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • And how would he accomplish this using the Windows Phone API? – Anya Shenanigans Aug 16 '13 at 09:43
  • The usual way.. web api is via a url call, json to read the reply from the web service, which is available to Windows Phone apps. Look at http://www.geekchamp.com/articles/getting-latest-tweets-in-windows-phone-apps-using-twitter-rest-api-and-json-net for some pointers P.S. This is a "help" website, not necessarily a "post a complete answer website". – Paul Zahra Aug 16 '13 at 14:09
  • 1
    @PaulZahra, I think what Petesh means is that there is no concept of a "windows service" in Windows Phone; not that there isn't a Web client. – New Dev Aug 16 '13 at 22:25
  • My bad, I didn't think i implied a Windows service would run on a Windows Phone. Typically Windows Services are an exe that is perfect for servers and run in the background on such, see http://msdn.microsoft.com/en-us/library/d56de412.aspx for what a Windows Service actually is. The Web API part would in effect allow you to query the Windows Service. – Paul Zahra Aug 19 '13 at 07:37