// 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; namespace InvokeAgain { public partial class MainForm : Form { private int val; private Label lbl; private Label cnt; public MainForm() { InitializeComponent(); Size = new Size(450, 150); lbl = new Label(); lbl.Location = new Point(50, 50); lbl.Size = new Size(150, 50); lbl.Text = "Counter: "; Controls.Add(lbl); cnt = new Label(); cnt.Location = new Point(250, 50); cnt.Size = new Size(150, 50); cnt.Text = val.ToString(); Controls.Add(cnt); new Thread(CounterUpdate).Start(); } public void CounterUpdate() { while(true) { Thread.Sleep(1000); val++; if(cnt.InvokeRequired) { cnt.Invoke(new Action(() => cnt.Text = val.ToString())); } else { cnt.Text = val.ToString(); } } } } public class Program { [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } } }