// 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.Drawing; using System.Windows.Forms; using System.Threading; public class MainForm : Form { private Label time; private Button start; private Button abort; private Thread t; public MainForm() { time = new Label(); start = new Button(); abort = new Button(); SuspendLayout(); time.Location = new Point(50, 50); time.Size = new Size(300, 50); time.Font = new Font(FontFamily.GenericSerif, 16.0f); start.Location = new Point(50,150); start.Size = new Size(200, 50); start.Name = "Start Button"; start.Text = "Start clock"; start.Click += StartClick; abort.Location = new Point(50,250); abort.Size = new Size(200, 50); abort.Name = "Abort Button"; abort.Text = "Abort clock"; abort.Click += AbortClick; ClientSize = new Size(400, 350); Controls.Add(time); Controls.Add(start); Controls.Add(abort); Name = "Main Form"; Text = "Main Form"; ResumeLayout(false); } public void Run() { while(true) { if(time.InvokeRequired) { time.Invoke((Action)(() => time.Text = DateTime.Now.ToString())); } else { Update(); } Thread.Sleep(1000); } } public void StartClick(object sender, EventArgs e) { t = new Thread(Run); t.Start(); } public void AbortClick(object sender, EventArgs e) { t.Abort(); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); Application.Exit(); Environment.Exit(0); } }