Guest User

Untitled

a guest
Sep 12th, 2019
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4.  
  5. namespace Sandbox
  6. {
  7. public class ExitCompare : Form
  8. {
  9. Thread BackgroundThread = null;
  10.  
  11. public ExitCompare()
  12. {
  13. SuspendLayout();
  14.  
  15. var table = new TableLayoutPanel { Dock = DockStyle.Fill, RowCount = 3, ColumnCount = 1 };
  16. table.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33f));
  17. table.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33f));
  18. table.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33f));
  19. Controls.Add(table);
  20.  
  21. var startThreadButton = new Button
  22. {
  23. Dock = DockStyle.Fill,
  24. TabIndex = 0,
  25. Text = "Start background thread"
  26. };
  27. startThreadButton.Click += StartThread;
  28. table.Controls.Add(startThreadButton, 0, 0);
  29.  
  30. var applicationExitButton = new Button
  31. {
  32. Dock = DockStyle.Fill,
  33. TabIndex = 1,
  34. Text = "Application.Exit"
  35. };
  36. applicationExitButton.Click += (sender, e) => Application.Exit();
  37. table.Controls.Add(applicationExitButton, 0, 1);
  38.  
  39. var environmentExitButton = new Button
  40. {
  41. Dock = DockStyle.Fill,
  42. TabIndex = 2,
  43. Text = "Environment.Exit"
  44. };
  45. environmentExitButton.Click += (sender, e) => Environment.Exit(0);
  46. table.Controls.Add(environmentExitButton, 0, 2);
  47.  
  48. this.Text = "Exit Comparison";
  49. this.ResumeLayout(false);
  50. }
  51.  
  52. private void StartThread(object sender, EventArgs e)
  53. {
  54. if (BackgroundThread == null)
  55. {
  56. BackgroundThread = new Thread(new ThreadStart(() =>
  57. {
  58. while (true)
  59. {
  60. Thread.Sleep(1000);
  61. Console.WriteLine("Background thread running.");
  62. }
  63. }));
  64. BackgroundThread.Start();
  65. }
  66.  
  67. ((Button)sender).Enabled = false;
  68. }
  69.  
  70. [STAThread]
  71. public static void Main()
  72. {
  73. Application.EnableVisualStyles();
  74. Application.SetCompatibleTextRenderingDefault(false);
  75. Application.Run(new ExitCompare());
  76. }
  77. }
  78. }
RAW Paste Data