Timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poko
    New Member
    • Dec 2009
    • 16

    Timer

    I want a textbox on the form to show the current time. The code I have written follows. But it does not work and I don't know why :( Can someone please point out the error.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Timers;
    using System.Threading;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private System.Timers.Timer myTimer = new System.Timers.Timer();
            private TextBox textBox2;
    
            private System.ComponentModel.IContainer components = null;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(84, 186);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(100, 20);
                this.textBox2.TabIndex = 0;
                this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
                // 
                // Form1
                // 
                this.ClientSize = new System.Drawing.Size(284, 264);
                this.Controls.Add(this.textBox2);
                this.Name = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
                this.Load += new System.EventHandler(this.Form1_Load);
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                    myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
                    myTimer.Interval = 1000;
                    myTimer.Start();
            }
    
            public void DisplayTimeEvent( object source, ElapsedEventArgs e )
            {
                textBox2.Text = DateTime.Now.ToString();
            }
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
    
            }
        }
    }
    Last edited by tlhintoq; Dec 18 '09, 12:01 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Put a breakpoint on line 67 then run/debug/F5 the project.
      If the execution breaks on the line, then you know your timer is running and the event is being called.
      If execution doesn't ever break on this line, then you point to start searching backward from:
      IE: The handler is never called, therefore the Timer.Elapsed event never raises

      Personally, for something as course as 1,000 miliseconds I'd just use a Windows.Forms.T imer and the .Tick event.

      Comment

      • poko
        New Member
        • Dec 2009
        • 16

        #4
        I'll keep the [code] tags in mind from next time. I put a breakpoint on line 67, but the execution did not break. So I guess that the handler is not getting called. Do you know why could that be happening?

        What I really want to create is a countdown timer for an elapsed time. So I trying the get the basic up-count timer right before I do that.

        Comment

        • poko
          New Member
          • Dec 2009
          • 16

          #5
          I managed to execute a call to a handler...but now I am getting another error:

          $exception {"Cross-thread operation not valid: Control 'textBox2' accessed from a thread other than the thread it was created on."} System.Exceptio n {System.Invalid OperationExcept ion}

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Well there ya go... The timer and the form have not been created on the same thread.

            Let me again suggest using a System.Windows. Forms.Timer and making that timer a control on Form1

            For a countdown timer you can create a new DateTime object set to a time in the future then subtract DateTime.Now from that future object. The difference is the remaining time which you will display as you countdown.

            Comment

            • poko
              New Member
              • Dec 2009
              • 16

              #7
              Yo....I got it to work. Thanks :)

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Congratulations ! Since this is a place of learning and sharing, would you mind sharing your working solution with the rest of the forum in case someone else (now or in the future) has a similar problem?

                Comment

                • poko
                  New Member
                  • Dec 2009
                  • 16

                  #9
                  Sure

                  Code:
                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Data;
                  using System.Drawing;
                  using System.Linq;
                  using System.Text;
                  using System.Windows.Forms;
                  using System.Timers;
                  using System.Threading;
                  
                  namespace WindowsFormsApplication1
                  {
                      public partial class Form1 : Form
                      {
                          private TextBox textBox2;
                          private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
                          
                          private System.ComponentModel.IContainer components = null;
                  
                          public Form1()
                          {
                              InitializeComponent();
                          }
                  
                          protected override void Dispose(bool disposing)
                          {
                              if (disposing && (components != null))
                              {
                                  components.Dispose();
                              }
                              base.Dispose(disposing);
                          }
                  
                          private void InitializeComponent()
                          {
                              this.textBox2 = new System.Windows.Forms.TextBox();
                              this.SuspendLayout();
                              // 
                              // textBox2
                              // 
                              this.textBox2.Location = new System.Drawing.Point(84, 186);
                              this.textBox2.Name = "textBox2";
                              this.textBox2.Size = new System.Drawing.Size(100, 20);
                              this.textBox2.TabIndex = 0;
                              this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
                              // 
                              // Form1
                              // 
                              this.ClientSize = new System.Drawing.Size(284, 264);
                              this.Controls.Add(this.textBox2);
                              this.Name = "Form1";
                              this.ResumeLayout(false);
                              this.PerformLayout();
                              this.Load += new System.EventHandler(this.Form1_Load);
                          }
                  
                          private void Form1_Load(object sender, EventArgs e)
                          {
                                  ElapsedEventHandler(DisplayTimeEvent);
                                  myTimer.Interval = 1000;
                                  myTimer.Start();
                                  myTimer.Tick += new EventHandler(DisplayTimeEvent);
                          }
                  
                          public void DisplayTimeEvent( object sender, EventArgs e )
                          {
                              this.textBox2.Text = DateTime.Now.ToString();
                          }
                  
                          private void textBox2_TextChanged(object sender, EventArgs e)
                          {
                  
                          }
                      }
                  }

                  Comment

                  Working...