Timer stopping in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tawanda diza
    New Member
    • Sep 2011
    • 29

    Timer stopping in c#

    hie all.

    i am new to c#, ma y you please help me on how to stop a timer. i created a timer during design time.

    i am looking at some sources here and they say i must use
    Code:
    timer.stop();
    but when i try to use it i am getting an error saying
    Code:
    "error CS1061: 'System.Windows.Forms.Timer' does not contain a definition for 'stop' and no extension method 'stop' accepting a first argument of type 'System.Windows.Forms.Timer' could be found (are you missing a using directive or an assembly reference?)
    "
    what i am trying to accomplish is that my dialog must close after a certain time, if i use
    Code:
    timer.Enabled = false;
    it is not stopping the timer,

    i wanted the timer to stop completely when i click a button on that dialog and i am puttting a
    Code:
    timer.enabled =  false
    on the click event

    thanks in advance
  • Fr33dan
    New Member
    • Oct 2008
    • 57

    #2
    The Stop function of the Timer class does not follow the typical "first letter of methods is lowercase" practice. That being said, setting timer.Enabled to false should stop it anyway. Is still running or are you just getting a single fire after the fact? Or maybe due to the way you programmed the dialog you're accessing the wrong timer?

    Comment

    • adriancs
      New Member
      • Apr 2011
      • 122

      #3
      you may try this:
      Code:
      public partial class Form1 : Form
      {
          Timer timer1;
          Timer timer2;
          public Form1()
          {
              InitializeComponent();
              timer1 = new Timer();
              timer1.Interval = 3000;
              timer1.Tick += new EventHandler(timer1_Tick);
              timer2 = new Timer();
              timer2.Interval = 200;
              timer2.Tick += new EventHandler(timer2_Tick);
          }
      
          private void button1_Click(object sender, EventArgs e)
          {
              timer1.Start();
              timer2.Start();
          }
      
          void timer2_Tick(object sender, EventArgs e)
          {
              this.label1.Text += "\nHello World";
          }
      
          void timer1_Tick(object sender, EventArgs e)
          {
              timer1.Stop();
              timer2.Stop();
              MessageBox.Show("Stop saying hello.");
          }
      }

      Comment

      Working...