9 Replies - 10290 Views - Last Post: 23 May 2011 - 09:54 AM

#1 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

How do you create a countdown timer in WP7?

Posted 12 April 2011 - 12:39 PM

I am trying to create a timer that starts out at 6 seconds, and counts down to zero. My code below starts the timer when the button is clicked but it currently only displays the seconds portion of the current date/time. How do you set it up to start at a given number then count down?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using Microsoft.Phone.Controls;

namespace timersExperiment
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Timer myTimer = new Timer(MyTimerCallback, outputBlock, 1000, 1000);

        }

        private static void MyTimerCallback(object state)
        {
            TextBlock outputBlock = (TextBlock)state;
            string msg = DateTime.Now.ToString("ss") + "\n";
            
            outputBlock.Dispatcher.BeginInvoke(delegate() { outputBlock.Text = msg.ToString(); });
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: How do you create a countdown timer in WP7?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: How do you create a countdown timer in WP7?

Posted 12 April 2011 - 12:42 PM

Simply create a class level variable that keeps the number that you want to show, then subtract 1 from it every second.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using Microsoft.Phone.Controls;

namespace timersExperiment
{
    public partial class MainPage : PhoneApplicationPage
    {
        int myCounter = 6;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Timer myTimer = new Timer(MyTimerCallback, outputBlock, 1000, 1000);

        }

        private static void MyTimerCallback(object state)
        {
            TextBlock outputBlock = (TextBlock)state;
            //string msg = DateTime.Now.ToString("ss") + "\n";
            string msg = Convert.ToString(myCounter--);            

            outputBlock.Dispatcher.BeginInvoke(delegate() { outputBlock.Text = msg; });
        }
    }
}



That should work.
Was This Post Helpful? 1
  • +
  • -

#3 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: How do you create a countdown timer in WP7?

Posted 12 April 2011 - 12:54 PM

Thank you for your quick reply, I think I have it now, however I did need to declare the countdown variable as a static variable for your example to work. Works great now, at least I headed in the right direction.

Thanks again,
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: How do you create a countdown timer in WP7?

Posted 12 April 2011 - 01:04 PM

Sorry, didn't notice that your callback method was static.
Was This Post Helpful? 0
  • +
  • -

#5 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: How do you create a countdown timer in WP7?

Posted 19 May 2011 - 11:24 AM

Ok,

Thanks to your help I have this working, mostly. But when the countdown reaches zero, I need a few things to happen. Here is my current code:

public void MyTimerCallback(object state)
        {

            TextBlock outputBlock = (TextBlock)state;
            if (myCounter >= 0)
            {
                msg = Convert.ToString(myCounter--);

            }
            else
            {
                msg = "END";

                LetterArea.Visibility = System.Windows.Visibility.Collapsed;
                StartButton.Content = "Again?";
                StartButton.Visibility = System.Windows.Visibility.Visible;

            }
            outputBlock.Dispatcher.BeginInvoke(delegate() { outputBlock.Text = msg; });

        }



The problem I have is the
 LetterArea.Visibility = System.Windows.Visibility.Collapsed;

throws an exception I am unfamiliar with and cannot find information on. "UnauthorizedAccessException" how can I get this to work, what am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: How do you create a countdown timer in WP7?

Posted 19 May 2011 - 04:51 PM

You need to give all of the Exception. We need to know the actual message of the exception.

You are probably getting the error because you are interacting with a UI control from a thread that is not the UI thread. That visibility code needs to go inside of the delegate for the BeginInvoke.
Was This Post Helpful? 0
  • +
  • -

#7 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: How do you create a countdown timer in WP7?

Posted 20 May 2011 - 04:27 PM

View Posteclipsed4utoo, on 19 May 2011 - 05:51 PM, said:

You need to give all of the Exception. We need to know the actual message of the exception.

You are probably getting the error because you are interacting with a UI control from a thread that is not the UI thread. That visibility code needs to go inside of the delegate for the BeginInvoke.



Actually, that is the entire exception, that is why I am having trouble finding a solution to the problem, exception is vague.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: How do you create a countdown timer in WP7?

Posted 21 May 2011 - 05:22 AM

You are interacting with controls from a background thread. This isn't allowed. Your code should probably look like this...

public void MyTimerCallback(object state)
{

	TextBlock outputBlock = (TextBlock)state;
	if (myCounter >= 0)
		msg = Convert.ToString(myCounter--);
	else
		msg = "END";

	outputBlock.Dispatcher.BeginInvoke(delegate() 
	{ 
		   outputBlock.Text = msg; 

		   if (myCounter >= 0)
		   {
				LetterArea.Visibility = System.Windows.Visibility.Collapsed;
				StartButton.Content = "Again?";
				StartButton.Visibility = System.Windows.Visibility.Visible;

		   }
	});
}


Was This Post Helpful? 2
  • +
  • -

#9 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: How do you create a countdown timer in WP7?

Posted 23 May 2011 - 07:53 AM

Thanks for the help! I am having one other problem with this, With your help I have been able to get the timer working and doing what I want it to do, however it only works if nothing else is happening, for instance while the timer is running, and say you click one of the buttons on the screen, the timer stops, and does not restart. How can the other controls effect the timer without the timer being part of their respective click events?
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: How do you create a countdown timer in WP7?

Posted 23 May 2011 - 09:54 AM

It should work fine....unless you are disabling the timer in the code for the buttons. Without seeing your code, it would be hard to say.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1