Answers for "does timer loop c#"

C#
0

c# wpf timer

using System;
using System.Windows;
using System.Windows.Threading;

namespace WpfTutorialSamples.Misc
{
	public partial class DispatcherTimerSample : Window
	{
		public DispatcherTimerSample()
		{
			InitializeComponent();
			DispatcherTimer timer = new DispatcherTimer();
			timer.Interval = TimeSpan.FromSeconds(1);
			timer.Tick += timer_Tick;
			timer.Start();
		}

		void timer_Tick(object sender, EventArgs e)
		{
			lblTime.Content = DateTime.Now.ToLongTimeString();
		}
	}
}
Posted by: Guest on December-11-2020
2

c# timer program

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace Countdown
{
    public partial class Form1 : Form
    {
        // Follow the video to set up the rest of the project:
        System.Timers.Timer t;
        int h = 0, m = 0, s = 0, ms = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void txtResult_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            s = 1;
            t = new System.Timers.Timer();
            t.Interval = 10;
            t.Elapsed += OnTimeEvent;
        }

        private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            Invoke(new Action(() =>
            {
                ms -= 1;
                if (ms < 0)
                {
                    ms = 60;
                    s -= 1;
                }
                if (s < 0)
                {
                    s = 60;
                    m -= 1;
                }
                if (m < 0)
                {
                    m = 60;
                    h -= 1;
                }
                if (h < 0)
                {
                    h = 60;
                }
                if (ms == 0 && s == 0 && m == 0 && h == 0)
                {
                    t.Stop();
                }
                txtResult.Text = string.Format("{0}:{1}:{2}:{3}", h.ToString().PadLeft(2, '0'), m.ToString().PadLeft(2, '0'), s.ToString().PadLeft(2, '0'), ms.ToString().PadLeft(2, '0'));
            }));
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            t.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            t.Stop();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            t.Stop();
            Application.DoEvents();
        }
    }
}
Posted by: Guest on August-29-2021

C# Answers by Framework

Browse Popular Code Answers by Language