Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 7k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Threading;
  8. namespace ch9_6
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.Label label1;
  16. private System.Windows.Forms.Button button1;
  17. private System.Windows.Forms.Label label2;
  18. private System.Windows.Forms.Label label4;
  19. private System.Windows.Forms.Label label3;
  20. private System.Windows.Forms.ComboBox comboBox1;
  21. private System.Windows.Forms.ComboBox comboBox2;
  22. private System.Windows.Forms.RichTextBox richTextBox1;
  23. private System.Windows.Forms.Label label5;
  24. /// <summary>
  25. /// 必需的设计器变量。
  26. /// </summary>
  27. private System.ComponentModel.Container components = null;
  28. public Form1()
  29. {
  30. //
  31. // Windows 窗体设计器支持所必需的
  32. //
  33. InitializeComponent();
  34. this.comboBox1.SelectedIndex=1;
  35. this.comboBox2.SelectedIndex=1;
  36. //
  37. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  38. //
  39. }
  40. /// <summary>
  41. /// 清理所有正在使用的资源。
  42. /// </summary>
  43. protected override void Dispose( bool disposing )
  44. {
  45. if( disposing )
  46. {
  47. if (components != null) 
  48. {
  49. components.Dispose();
  50. }
  51. }
  52. base.Dispose( disposing );
  53. }
  54. #region Windows Form Designer generated code
  55. /// <summary>
  56. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  57. /// 此方法的内容。
  58. /// </summary>
  59. private void InitializeComponent()
  60. {
  61. this.label1 = new System.Windows.Forms.Label();
  62. this.button1 = new System.Windows.Forms.Button();
  63. this.label2 = new System.Windows.Forms.Label();
  64. this.label4 = new System.Windows.Forms.Label();
  65. this.label3 = new System.Windows.Forms.Label();
  66. this.comboBox1 = new System.Windows.Forms.ComboBox();
  67. this.comboBox2 = new System.Windows.Forms.ComboBox();
  68. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  69. this.label5 = new System.Windows.Forms.Label();
  70. this.SuspendLayout();
  71. // 
  72. // label1
  73. // 
  74. this.label1.Location = new System.Drawing.Point(16, 16);
  75. this.label1.Name = "label1";
  76. this.label1.Size = new System.Drawing.Size(88, 16);
  77. this.label1.TabIndex = 0;
  78. this.label1.Text = "线程1当前数字";
  79. // 
  80. // button1
  81. // 
  82. this.button1.Location = new System.Drawing.Point(200, 72);
  83. this.button1.Name = "button1";
  84. this.button1.Size = new System.Drawing.Size(72, 23);
  85. this.button1.TabIndex = 1;
  86. this.button1.Text = "开始";
  87. this.button1.Click += new System.EventHandler(this.button1_Click);
  88. // 
  89. // label2
  90. // 
  91. this.label2.Location = new System.Drawing.Point(128, 16);
  92. this.label2.Name = "label2";
  93. this.label2.Size = new System.Drawing.Size(48, 16);
  94. this.label2.TabIndex = 2;
  95. // 
  96. // label4
  97. // 
  98. this.label4.Location = new System.Drawing.Point(128, 48);
  99. this.label4.Name = "label4";
  100. this.label4.Size = new System.Drawing.Size(48, 16);
  101. this.label4.TabIndex = 6;
  102. // 
  103. // label3
  104. // 
  105. this.label3.Location = new System.Drawing.Point(16, 48);
  106. this.label3.Name = "label3";
  107. this.label3.Size = new System.Drawing.Size(88, 16);
  108. this.label3.TabIndex = 5;
  109. this.label3.Text = "线程2当前数字";
  110. // 
  111. // comboBox1
  112. // 
  113. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  114. this.comboBox1.Items.AddRange(new object[] {
  115.    "高",
  116.    "正常",
  117.    "低"});
  118. this.comboBox1.Location = new System.Drawing.Point(200, 8);
  119. this.comboBox1.Name = "comboBox1";
  120. this.comboBox1.Size = new System.Drawing.Size(72, 20);
  121. this.comboBox1.TabIndex = 7;
  122. // 
  123. // comboBox2
  124. // 
  125. this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  126. this.comboBox2.Items.AddRange(new object[] {
  127.    "高",
  128.    "正常",
  129.    "低"});
  130. this.comboBox2.Location = new System.Drawing.Point(200, 40);
  131. this.comboBox2.Name = "comboBox2";
  132. this.comboBox2.Size = new System.Drawing.Size(72, 20);
  133. this.comboBox2.TabIndex = 8;
  134. // 
  135. // richTextBox1
  136. // 
  137. this.richTextBox1.Location = new System.Drawing.Point(0, 96);
  138. this.richTextBox1.Name = "richTextBox1";
  139. this.richTextBox1.Size = new System.Drawing.Size(288, 312);
  140. this.richTextBox1.TabIndex = 9;
  141. this.richTextBox1.Text = "";
  142. // 
  143. // label5
  144. // 
  145. this.label5.Location = new System.Drawing.Point(16, 80);
  146. this.label5.Name = "label5";
  147. this.label5.Size = new System.Drawing.Size(100, 16);
  148. this.label5.TabIndex = 10;
  149. this.label5.Text = "结果显示";
  150. // 
  151. // Form1
  152. // 
  153. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  154. this.ClientSize = new System.Drawing.Size(292, 421);
  155. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  156.   this.label5,
  157.   this.richTextBox1,
  158.   this.comboBox2,
  159.   this.comboBox1,
  160.   this.label4,
  161.   this.label3,
  162.   this.label2,
  163.   this.button1,
  164.   this.label1});
  165. this.Name = "Form1";
  166. this.Text = "多线程调度";
  167. this.ResumeLayout(false);
  168. }
  169. #endregion
  170. /// <summary>
  171. /// 应用程序的主入口点。
  172. /// </summary>
  173. [STAThread]
  174. static void Main() 
  175. {
  176. Application.Run(new Form1());
  177. }
  178. public void HandleThread1()
  179. {
  180. for (int i=0;i<10;i++)
  181. {
  182. for (int j=1;j<100;j++)
  183. {
  184. this.label2.Text=i.ToString();
  185. }
  186. //在richTextBox1中输出
  187. this.richTextBox1.Text+="来自线程1的数字:"+i.ToString()+"n";
  188. }
  189. }
  190. public void HandleThread2()
  191. {   
  192. for (int i=0;i<10;i++)
  193. {
  194. for (int j=1;j<100;j++)
  195. {
  196. this.label4.Text=i.ToString();
  197. }
  198. //在richTextBox1中输出
  199. this.richTextBox1.Text+="来自线程2的数字:"+i.ToString()+"n";
  200. }
  201. }
  202. private void button1_Click(object sender, System.EventArgs e)
  203. {
  204. //创建线程1和线程2的实例
  205. Thread th1=new Thread(new ThreadStart(HandleThread1));
  206. Thread th2=new Thread(new ThreadStart(HandleThread2));
  207. this.richTextBox1.Text=""; 
  208. int i=this.comboBox1.SelectedIndex;
  209.     //设置线程1优先级
  210.   switch (i.ToString())
  211. {
  212. case "0":
  213. th1.Priority=ThreadPriority.AboveNormal;
  214. break;
  215. case "1":
  216. th1.Priority=ThreadPriority.Normal;
  217. break;
  218. case "2":
  219. th1.Priority=ThreadPriority.BelowNormal;
  220. break;
  221. }
  222. int j=this.comboBox2.SelectedIndex;
  223. //设置线程2优先级
  224. switch (j.ToString())
  225. {
  226. case "0":
  227. th2.Priority=ThreadPriority.AboveNormal;
  228. break;
  229. case "1":
  230. th2.Priority=ThreadPriority.Normal;
  231. break;
  232. case "2":
  233. th2.Priority=ThreadPriority.BelowNormal;
  234. break;
  235. }
  236.         //启动线程
  237. th1.Start();
  238. th2.Start();
  239. }
  240. }
  241. }