Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining event handler for Tick event of DispatcherTimer in windows 8 app

Tags:

windows-8

I am developing an application in windows 8 Visual studio 11, and I want to define an event handler for a DispatcherTimer instance as below:

public sealed partial class BlankPage : Page
    {

        int timecounter = 10;
        DispatcherTimer timer = new DispatcherTimer();
        public BlankPage()
        {
            this.InitializeComponent();
            timer.Tick += new EventHandler(HandleTick);
        }

        private void HandleTick(object s,EventArgs e)
        {

            timecounter--;
            if (timecounter ==0)
            {
                //disable all buttons here
            }
        }
        .....
}

But I get the following Error :

Cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<object>'

I am a novice developer to widows 8 apps.

Would you please help me ?

like image 883
Babak Fakhriloo Avatar asked May 05 '12 17:05

Babak Fakhriloo


People also ask

How do you use a tick timer?

Pull the tape slowly and switch the ticker-timer on at the start signal. Switch it off at the stop signal. Count the number of dot-to-dot spaces between the start and the stop. That is the time between the signals, measured in ticks .

What is timer tick?

The Stopwatch. ElapsedTicks is measuring a "tick" in terms of the stopwatch, which is a length of time of 1 second/ Stopwatch.

Which of the following control have tick event?

When the interval elapses in timer control, the Elapsed event has occurred. A tick event is used to repeat the task according to the time set in the Interval property. It is the default event of a timer control that repeats the task between the Start() and Stop() methods.


2 Answers

almost had it :) You don't need to instantiate a new eventhandler object, you only need to point to the method that handles the event. Hence, an eventhandler.

        int timecounter = 10;
    DispatcherTimer timer = new DispatcherTimer();
    public BlankPage()
    {
        this.InitializeComponent();

        timer.Tick += timer_Tick;
    }

    protected void timer_Tick(object sender, object e)
    {
        timecounter--;
        if (timecounter == 0)
        {
            //disable all buttons here
        }
    }

Try to read up on delegates to understand events Understanding events and event handlers in C#

like image 102
danielovich Avatar answered Oct 29 '22 01:10

danielovich


Your code is expecting HandleTick to have two Object params. Not an object param and an EventArg param.

private void HandleTick(object s, object e)

NOT

private void HandleTick(object s,EventArgs e)

This is a change that took place for Windows 8.

like image 36
dannybrown Avatar answered Oct 29 '22 00:10

dannybrown