3

Am currently building an app and I want the timer to start only when I click a particular button.

So is there anyway to start timer once a button is clicked? (I don't want the timer to start as soon as the page loads)

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44

2 Answers2

4

Check this post.

//Inside Page Load 
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
dt.Tick += new EventHandler(dt_Tick);

Tick Event Handler for your timer

void dt_Tick(object sender, EventArgs e)
{
    // Do Stuff here.
}

Now on your button click event handler you will do

dt.Start();

Hope this helps.

Community
  • 1
  • 1
Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
  • what type of classes or references should I include??! @Amar Palsapure – F 505 Nov 23 '13 at 15:04
  • @F505 have you checked http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.100).aspx. You can check the .net framework also. – Amar Palsapure Nov 24 '13 at 07:58
0

By default the Enabled property of timer is false. Hence it will not start on load.
You can have a button click event which starts the timer on clicking it.

    private void btnStartTimer_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

Add a Timer Tick event which confirms that the timer has started.

    private void timer1_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("Timer Tick");
    }
Manjunath K Mayya
  • 1,078
  • 1
  • 11
  • 20