fork download
  1. public partial class Form1 : Form
  2. {
  3. private Point _defaultLocation;
  4.  
  5. private Thread _trd;
  6. private delegate void MoveTextBoxDelegate();
  7. private volatile bool _canceled = false;
  8.  
  9. public Form1()
  10. {
  11. InitializeComponent();
  12.  
  13. this._defaultLocation = this.textBox1.Location;
  14. }
  15.  
  16. private void button1_Click(object sender, EventArgs e)
  17. {
  18. //既に移動処理中か?
  19. if (this._trd != null && this._trd.IsAlive)
  20. {
  21. //移動処理を途中で終わらす
  22. //this._trd.Abort(); //意図した動きをしないため使用しない
  23. this._canceled = true;
  24. this._trd.Join(); //!!! Application.DoEventsで止まっているためデッドロック発生 !!!
  25. this._canceled = false;
  26. }
  27.  
  28. //移動処理用スレッドをスタートする
  29. this._trd = new Thread(MoveTextBoxWorker);
  30. this._trd.IsBackground = true;
  31. this._trd.Start();
  32. }
  33.  
  34. private void MoveTextBoxWorker()
  35. {
  36. this.Invoke(new MoveTextBoxDelegate(MoveTextBox));
  37. }
  38.  
  39. /// <summary>
  40. /// 移動処理メソッド
  41. /// </summary>
  42. private void MoveTextBox()
  43. {
  44. Int32 moveDistance = 1;
  45.  
  46. this.textBox1.Location = this._defaultLocation;
  47.  
  48. while (this.textBox1.Location.X <= this.Width)
  49. {
  50. this.textBox1.Location = new Point(this.textBox1.Location.X + moveDistance, this.textBox1.Location.Y);
  51. this.textBox1.Update();
  52.  
  53. moveDistance = moveDistance * 2;
  54. Thread.Sleep(100);
  55.  
  56. Application.DoEvents(); //途中でもbutton1_Clickイベントを割り込ますため
  57.  
  58. if (this._canceled)
  59. {
  60. break;
  61. }
  62. }
  63. }
  64.  
  65. /// <summary>
  66. /// デバッグ用の情報が入っているtextBox1のTextをクリアする。
  67. /// </summary>
  68. /// <param name="sender"></param>
  69. /// <param name="e"></param>
  70. private void button2_Click(object sender, EventArgs e)
  71. {
  72. this.textBox1.Clear();
  73. }
  74. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty