// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Threading.Tasks; namespace E { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } // the crappy solution void Button1Click(object sender, EventArgs e) { while(true) { label1.Text = DateTime.Now.ToString("HH:mm:ss"); System.Threading.Thread.Sleep(1000); Application.DoEvents(); } } // the traditional solution void Update2() { label2.Text = DateTime.Now.ToString("HH:mm:ss"); } void Run() { while(true) { if(label2.InvokeRequired) { label2.Invoke((Action)(() => Update2())); } else { Update2(); } System.Threading.Thread.Sleep(1000); } } void Button2Click(object sender, EventArgs e) { (new System.Threading.Thread(Run)).Start(); } // the win forms solution void Update3(object sender, EventArgs e) { label3.Text = DateTime.Now.ToString("HH:mm:ss"); } Timer t; void Button3Click(object sender, EventArgs e) { t = new Timer(); t.Interval = 1000; t.Tick += Update3; t.Start(); } // the async solution async void Button4Click(object sender, EventArgs e) { while(true) { label4.Text = DateTime.Now.ToString("HH:mm:ss"); await Task.Delay(1000); } } // void Button5Click(object sender, EventArgs e) { Application.Exit(); Environment.Exit(0); } } }