0

This is my second question on StackOverflow here. I posted my first question a while ago and got a working reply in no time, much impressed, much appreciated.

Anyways, so what I want to know is, how to get a DispatcherTimer to work and show time in a certain textbox and stop it when it reaches a certain time (let's say 60 seconds) and perform a function after 60 seconds.

What I'm basically using this for is :

Making a game, which has to stop after 60 seconds and show the scores or related stuff. So this requires me to show the time in a textbox and perform a function at 60 seconds or after that.

Here's more information :

Textbox is called "timerbox"

Here's the code I've tried :

    DispatcherTimer dt = new DispatcherTimer();

    private void TimerStart(object sender, RoutedEventArgs e)
    {
        dt.Interval = TimeSpan.FromSeconds(1);
        dt.Tick += dt_Tick;
        dt.Start();
    }

    int count = 0;
    void dt_Tick(object sender, object e)
    {
        count = count + 1;
        timerbox.Text = Convert.ToString(count);
    }

It doesn't show the time in textbox, plus I don't know how to make it stop at certain point and perform a function.

Thank you for reaching here, please leave answers with full explanation as I'm a complete beginner :)

P.S. I'm using Windows Store App Development Environment in Visual Studio 2013. And there's no "Timer" in it as there is in normal C# Environment.

Hardik
  • 11
  • 4

2 Answers2

0

AOA. I am recently started learning c#. (interested in windows form application). Hope this help you.

if you just want to set timer for a curtain event.....

recommend you using timer ( in toolbox )......

follow steps, when you double click on timer1 VS will create a timer1_Tick function for you which will be called every timer you timer ticks..... now what you want to do when timer1 icks write it in there....like this....

private void timer1_Tick(object sender, EventArgs e)
    {
        //enter your code here
    }

now write timer1. and VS will display a list of avaliable function.... for example,

timer1.Interval = (60*1000); //enter time in milliseconds

now when you want to start the write......

timer1.Start();

and to stop timer at any timer call

timer1.Stop();

if you want to repeat timer just write timer1.start() in that tick function.....

plus, to set textbox text equal to timer1 time use something like

textBox1.Text = Convert.ToString(timer1.Interval);

Click here for more information on timer class

hope this help you, in case of any confusion, just comment,.....

ARK
  • 229
  • 1
  • 11
  • I forgot to mention above that there's no Timer in Windows Store App Dev Environment. We can only use DispatcherTimer for the purpose. But I tried to use your code, as you said, and it didn't work. Thanks though :) – Hardik Jul 28 '14 at 09:21
  • @Hardik Sorry, for this but i assumed that you are working in normal C# winodows form application develp.,.... – ARK Jul 31 '14 at 13:25
0

The normal flow of a DispatcherTimer would look like this:

  1. First Set up your new Object, set up the a new EventHandler that will run your desired code each Tick and Set the Timespan for the desired Tick Interval.

    public MainPage()
    {
        this.InitializeComponent();            
    
        timer = new DispatcherTimer();
        timer.Tick += new EventHandler<object>(timer_Tick);
        timer.Interval = TimeSpan.FromMilliseconds(bpm);
    }
    
  2. Set The Timer_Tick Envent

    async Void timer_Tick(object Sender, object e)
    {
    await this.Dispatcher.RunAsync(Windows.UI.core.CoreDispatcherPriority.High, () => 
    {
    //Run the Code
    textBox1.text = timer.interval.TotalMilliseconds.ToString();
    });
    
  3. You have to have a trigger to Start the Dispatcher(and to stop if you need to), for example a button

    private void StartButton_Click() 
    {
    timer.Start();
    }
    
  4. This example was done using The new windows 10 Universal App platform within VS2015, but I think it should look about the same in a normal windows 8 App