fork download
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Threading;
  4.  
  5. namespace InvokeTest
  6. {
  7. public partial class Form1 : Form
  8. {
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. }
  13.  
  14. /* デザイナ側のDisposeに追加
  15.   protected override void Dispose(bool disposing)
  16.   {
  17.   if (disposing && th != null)
  18.   {
  19.   th.Interrupt();
  20.   th.Join();
  21.   }
  22.   ...
  23.   }
  24.   */
  25.  
  26. Thread th;
  27. private void button1_Click(object sender, EventArgs e)
  28. {
  29. if (th == null)
  30. {
  31. th = new Thread(() =>
  32. {
  33. try
  34. {
  35. String s;
  36. this.Invoke((MethodInvoker)(() => s = textBox1.Text));
  37. int st = 0;
  38. int.TryParse(textBox1.Text, out st);
  39. for (int i = st; ; i++)
  40. {
  41. this.Invoke((MethodInvoker)(() => { textBox1.Text = "" + i; }));
  42. //this.BeginInvoke((MethodInvoker)(() => { textBox1.Text = "" + i; }));
  43. }
  44. }
  45. catch (ThreadInterruptedException) { }
  46. });
  47. th.Start();
  48. button1.Text = "停止";
  49. }
  50. else
  51. {
  52. th.Interrupt();
  53. th.Join();
  54. th = null;
  55. button1.Text = "開始";
  56. }
  57. }
  58. }
  59. }
  60.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty