using System; using System.Windows.Forms; using System.Threading; namespace InvokeTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /* デザイナ側のDisposeに追加 protected override void Dispose(bool disposing) { if (disposing && th != null) { th.Interrupt(); th.Join(); } ... } */ Thread th; private void button1_Click(object sender, EventArgs e) { if (th == null) { th = new Thread(() => { try { String s; this.Invoke((MethodInvoker)(() => s = textBox1.Text)); int st = 0; int.TryParse(textBox1.Text, out st); for (int i = st; ; i++) { this.Invoke((MethodInvoker)(() => { textBox1.Text = "" + i; })); //this.BeginInvoke((MethodInvoker)(() => { textBox1.Text = "" + i; })); } } catch (ThreadInterruptedException) { } }); th.Start(); button1.Text = "停止"; } else { th.Interrupt(); th.Join(); th = null; button1.Text = "開始"; } } } }