// 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 E { public class MainForm : Form { private ProgressBar bar; private Button start; private Button abort; private Thread t; private volatile bool done; public MainForm() { bar = new ProgressBar(); start = new Button(); abort = new Button(); SuspendLayout(); bar.Location = new Point(50, 50); bar.Size = new Size(200, 50); bar.Minimum = 0; bar.Maximum = 100; bar.Value = 0; bar.Style = ProgressBarStyle.Continuous; start.Location = new Point(50,150); start.Size = new Size(200, 50); start.Text = "Start"; start.Click += StartClick; abort.Location = new Point(50,250); abort.Size = new Size(200, 50); abort.Text = "Abort"; abort.Click += AbortClick; ClientSize = new Size(300, 350); Controls.Add(bar); Controls.Add(start); Controls.Add(abort); Text = "Main Form"; ResumeLayout(false); start.Enabled = true; abort.Enabled = false; } private void Something() { if(InvokeRequired) { Invoke((MethodInvoker)delegate() { start.Enabled = false; abort.Enabled = true; } ); } else { start.Enabled = false; abort.Enabled = true; } for(int i = 0; i <= 100 && !done; i++) { if(bar.InvokeRequired) { bar.Invoke((MethodInvoker)delegate() { bar.Value = i; } ); } else { bar.Value = i; } Thread.Sleep(100); } if(InvokeRequired) { Invoke((MethodInvoker)delegate() { start.Enabled = true; abort.Enabled = false; } ); } else { start.Enabled = true; abort.Enabled = false; } } private void StartClick(object sender, EventArgs e) { t = new Thread(Something); t.Start(); done = false; } private void AbortClick(object sender, EventArgs e) { done = true; } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); Environment.Exit(0); } } }