473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Timer

16
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.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Timers;
  10. using System.Threading;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private System.Timers.Timer myTimer = new System.Timers.Timer();
  17.         private TextBox textBox2;
  18.  
  19.         private System.ComponentModel.IContainer components = null;
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         protected override void Dispose(bool disposing)
  27.         {
  28.             if (disposing && (components != null))
  29.             {
  30.                 components.Dispose();
  31.             }
  32.             base.Dispose(disposing);
  33.         }
  34.  
  35.         private void InitializeComponent()
  36.         {
  37.             this.textBox2 = new System.Windows.Forms.TextBox();
  38.             this.SuspendLayout();
  39.             // 
  40.             // textBox2
  41.             // 
  42.             this.textBox2.Location = new System.Drawing.Point(84, 186);
  43.             this.textBox2.Name = "textBox2";
  44.             this.textBox2.Size = new System.Drawing.Size(100, 20);
  45.             this.textBox2.TabIndex = 0;
  46.             this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
  47.             // 
  48.             // Form1
  49.             // 
  50.             this.ClientSize = new System.Drawing.Size(284, 264);
  51.             this.Controls.Add(this.textBox2);
  52.             this.Name = "Form1";
  53.             this.ResumeLayout(false);
  54.             this.PerformLayout();
  55.             this.Load += new System.EventHandler(this.Form1_Load);
  56.  
  57.         }
  58.  
  59.         private void Form1_Load(object sender, EventArgs e)
  60.         {
  61.                 myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
  62.                 myTimer.Interval = 1000;
  63.                 myTimer.Start();
  64.         }
  65.  
  66.         public void DisplayTimeEvent( object source, ElapsedEventArgs e )
  67.         {
  68.             textBox2.Text = DateTime.Now.ToString();
  69.         }
  70.  
  71.         private void textBox2_TextChanged(object sender, EventArgs e)
  72.         {
  73.  
  74.         }
  75.     }
  76. }
Dec 17 '09 #1

✓ answered by poko

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Timers;
  10. using System.Threading;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private TextBox textBox2;
  17.         private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
  18.  
  19.         private System.ComponentModel.IContainer components = null;
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         protected override void Dispose(bool disposing)
  27.         {
  28.             if (disposing && (components != null))
  29.             {
  30.                 components.Dispose();
  31.             }
  32.             base.Dispose(disposing);
  33.         }
  34.  
  35.         private void InitializeComponent()
  36.         {
  37.             this.textBox2 = new System.Windows.Forms.TextBox();
  38.             this.SuspendLayout();
  39.             // 
  40.             // textBox2
  41.             // 
  42.             this.textBox2.Location = new System.Drawing.Point(84, 186);
  43.             this.textBox2.Name = "textBox2";
  44.             this.textBox2.Size = new System.Drawing.Size(100, 20);
  45.             this.textBox2.TabIndex = 0;
  46.             this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
  47.             // 
  48.             // Form1
  49.             // 
  50.             this.ClientSize = new System.Drawing.Size(284, 264);
  51.             this.Controls.Add(this.textBox2);
  52.             this.Name = "Form1";
  53.             this.ResumeLayout(false);
  54.             this.PerformLayout();
  55.             this.Load += new System.EventHandler(this.Form1_Load);
  56.         }
  57.  
  58.         private void Form1_Load(object sender, EventArgs e)
  59.         {
  60.                 ElapsedEventHandler(DisplayTimeEvent);
  61.                 myTimer.Interval = 1000;
  62.                 myTimer.Start();
  63.                 myTimer.Tick += new EventHandler(DisplayTimeEvent);
  64.         }
  65.  
  66.         public void DisplayTimeEvent( object sender, EventArgs e )
  67.         {
  68.             this.textBox2.Text = DateTime.Now.ToString();
  69.         }
  70.  
  71.         private void textBox2_TextChanged(object sender, EventArgs e)
  72.         {
  73.  
  74.         }
  75.     }
  76. }

8 2515
tlhintoq
3,525 Expert 2GB
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.
Dec 17 '09 #2
tlhintoq
3,525 Expert 2GB
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.Timer and the .Tick event.
Dec 18 '09 #3
poko
16
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.
Dec 18 '09 #4
poko
16
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.Exception {System.InvalidOperationException}
Dec 18 '09 #5
tlhintoq
3,525 Expert 2GB
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.
Dec 18 '09 #6
poko
16
Yo....I got it to work. Thanks :)
Dec 18 '09 #7
tlhintoq
3,525 Expert 2GB
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?
Dec 18 '09 #8
poko
16
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Timers;
  10. using System.Threading;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private TextBox textBox2;
  17.         private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
  18.  
  19.         private System.ComponentModel.IContainer components = null;
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         protected override void Dispose(bool disposing)
  27.         {
  28.             if (disposing && (components != null))
  29.             {
  30.                 components.Dispose();
  31.             }
  32.             base.Dispose(disposing);
  33.         }
  34.  
  35.         private void InitializeComponent()
  36.         {
  37.             this.textBox2 = new System.Windows.Forms.TextBox();
  38.             this.SuspendLayout();
  39.             // 
  40.             // textBox2
  41.             // 
  42.             this.textBox2.Location = new System.Drawing.Point(84, 186);
  43.             this.textBox2.Name = "textBox2";
  44.             this.textBox2.Size = new System.Drawing.Size(100, 20);
  45.             this.textBox2.TabIndex = 0;
  46.             this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
  47.             // 
  48.             // Form1
  49.             // 
  50.             this.ClientSize = new System.Drawing.Size(284, 264);
  51.             this.Controls.Add(this.textBox2);
  52.             this.Name = "Form1";
  53.             this.ResumeLayout(false);
  54.             this.PerformLayout();
  55.             this.Load += new System.EventHandler(this.Form1_Load);
  56.         }
  57.  
  58.         private void Form1_Load(object sender, EventArgs e)
  59.         {
  60.                 ElapsedEventHandler(DisplayTimeEvent);
  61.                 myTimer.Interval = 1000;
  62.                 myTimer.Start();
  63.                 myTimer.Tick += new EventHandler(DisplayTimeEvent);
  64.         }
  65.  
  66.         public void DisplayTimeEvent( object sender, EventArgs e )
  67.         {
  68.             this.textBox2.Text = DateTime.Now.ToString();
  69.         }
  70.  
  71.         private void textBox2_TextChanged(object sender, EventArgs e)
  72.         {
  73.  
  74.         }
  75.     }
  76. }
Dec 18 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
7
by: Grahmmer | last post by:
I have a few timers that are added to a form at runtime. I can handle the event fine, but I cannot identify which timer fired. Is there a way to do this? Timer Creation: -------------...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
12
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
8
by: KnighT | last post by:
I have a .net service that runs a System.Threading.Timer. The delegate points to the function that the service should execute when the timer elapses. Problem: The timer is not ticking. I have...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
11
by: Hotrod2000 | last post by:
I'm quite new to programming but I'm having problems getting a timer to work in visual studio.net I've created a timer on a form, enabled it and then typed the following code (from the mdsn...
16
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.