• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 846
  • Last Modified:

display forms.timer winform c#

Hi guys,

I currently have a program running on a forms.timer every 5 minutes.
how can I have this timer displayed on the form at the same time?

any thoughts?
thanks!!
0
solarissf
Asked:
solarissf
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something like this:
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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private DateTime targetDT;

        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);

            this.Shown += new EventHandler(Form1_Shown);
       
        }

        void Form1_Shown(object sender, EventArgs e)
        {
            targetDT = DateTime.Now.AddMinutes(5);
            timer1.Start();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = targetDT.Subtract(DateTime.Now);
            if (ts.TotalMilliseconds > 0)
            {
                label1.Text = String.Format("{0}:{1}:{2}", ts.Hours.ToString("00"), ts.Minutes.ToString("00"), ts.Seconds.ToString("00"));
            }
            else
            {
                targetDT = DateTime.Now.AddMinutes(5);

                // ... do something or launch another thread from here ...
                Console.WriteLine("...Foo()...");

            }
        }

    }
}

Open in new window

0
 
Element1910Commented:
Try this:

Timer timer = new Timer();
        Label label = new Label();
        int minutes = 5;
        int seconds = 0;

        public Form1()
        {
            InitializeComponent();

            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = (1000) * (1);
            timer.Enabled = true;
            timer.Start();
            label.Location = new Point(100, 100);
            label.AutoSize = true;
            label.Text = String.Empty;

            this.Controls.Add(label);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (minutes == 0 && seconds == 0)
            {
                timer.Stop();
            }
            else
            {
                if (seconds == 0)
                {
                    seconds = 59;
                    minutes--;
                }
                else
                {
                    seconds--;
                }
            }
            label.Text = "Timer: " + minutes + ":" + seconds;
        }

Open in new window

0
 
jonnidipCommented:
I would suggest you to use 2 different times.
The first is the one you already have, and is set at 5 minutes.
When this timer starts, you start another timer with 1 second timeout.
The 1 sec. timer will write in a label, or in a textbox (as shown in previous comments), the remaining time. But this should be its only function.
At your 5 min. timer "tick" event remember to reset the 1 sec. timer, and the job is done.

Regards.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
solarissfAuthor Commented:
thanks everyone for all your suggestions.  I ended up using Element1910 and it works.  Although I'm sure all your methods worked.

thanks!!!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just be aware that the approach used by Element1910 will accumulate a timing error that grows larger and larger over time.  This is because the Timer is only guaranteed to not fire before the timer interval.  It technically fires at some indeterminate time after the specified interval and each time it may be different.  This leads to an accumulated error since you are decrementing a "second" counter assuming that the timer fires exactly each second.  How far off, and how it quickly it becomes noticeable depends on what the system is doing...
 
The approach I posted uses an absolute date/time and actually calculates how much time is left to that particular time so it is correct not matter how busy the system gets.  The timer interval can be any value, less than a second or more than a second, and it will still work.
0
 
solarissfAuthor Commented:
thanks idle
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now