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 = "開始";
}
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uV2luZG93cy5Gb3JtczsKdXNpbmcgU3lzdGVtLlRocmVhZGluZzsKCm5hbWVzcGFjZSBJbnZva2VUZXN0CnsKICAgIHB1YmxpYyBwYXJ0aWFsIGNsYXNzIEZvcm0xIDogRm9ybQogICAgewogICAgICAgIHB1YmxpYyBGb3JtMSgpCiAgICAgICAgewogICAgICAgICAgICBJbml0aWFsaXplQ29tcG9uZW50KCk7CiAgICAgICAgfQoKICAgICAgICAvKiDjg4fjgrbjgqTjg4rlgbTjga5EaXNwb3Nl44Gr6L+95YqgCiAgICAgICAgcHJvdGVjdGVkIG92ZXJyaWRlIHZvaWQgRGlzcG9zZShib29sIGRpc3Bvc2luZykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChkaXNwb3NpbmcgJiYgdGggIT0gbnVsbCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdGguSW50ZXJydXB0KCk7CiAgICAgICAgICAgICAgICB0aC5Kb2luKCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgLi4uCiAgICAgICAgfQogICAgICAgICovCgogICAgICAgIFRocmVhZCB0aDsKICAgICAgICBwcml2YXRlIHZvaWQgYnV0dG9uMV9DbGljayhvYmplY3Qgc2VuZGVyLCBFdmVudEFyZ3MgZSkKICAgICAgICB7CiAgICAgICAgICAgIGlmICh0aCA9PSBudWxsKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0aCA9IG5ldyBUaHJlYWQoKCkgPT4KICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICB0cnkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIFN0cmluZyBzOwogICAgICAgICAgICAgICAgICAgICAgICB0aGlzLkludm9rZSgoTWV0aG9kSW52b2tlcikoKCkgPT4gcyA9IHRleHRCb3gxLlRleHQpKTsKICAgICAgICAgICAgICAgICAgICAgICAgaW50IHN0ID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgaW50LlRyeVBhcnNlKHRleHRCb3gxLlRleHQsIG91dCBzdCk7CiAgICAgICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSBzdDsgOyBpKyspCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRoaXMuSW52b2tlKChNZXRob2RJbnZva2VyKSgoKSA9PiB7IHRleHRCb3gxLlRleHQgPSAiIiArIGk7IH0pKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vdGhpcy5CZWdpbkludm9rZSgoTWV0aG9kSW52b2tlcikoKCkgPT4geyB0ZXh0Qm94MS5UZXh0ID0gIiIgKyBpOyB9KSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgY2F0Y2ggKFRocmVhZEludGVycnVwdGVkRXhjZXB0aW9uKSB7IH0KICAgICAgICAgICAgICAgIH0pOwogICAgICAgICAgICAgICAgdGguU3RhcnQoKTsKICAgICAgICAgICAgICAgIGJ1dHRvbjEuVGV4dCA9ICLlgZzmraIiOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdGguSW50ZXJydXB0KCk7CiAgICAgICAgICAgICAgICB0aC5Kb2luKCk7CiAgICAgICAgICAgICAgICB0aCA9IG51bGw7CiAgICAgICAgICAgICAgICBidXR0b24xLlRleHQgPSAi6ZaL5aeLIjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQo=