⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 北大青鸟内部资料
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace ThreadTest
{

	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class frmMain : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox txtMinValue;
		private System.Windows.Forms.TextBox txtMaxValue;
		private System.Windows.Forms.TextBox txtThreadNumber;
		private System.Windows.Forms.TextBox txtOutput;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label lblBeginNumber;
		private System.Windows.Forms.Label lblEndNumber;
		private System.Windows.Forms.Label lblThreadNumber;
		private System.Windows.Forms.Button btnCalculate;
		private System.Windows.Forms.ListBox lstMessage;

		
		public frmMain()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnCalculate = new System.Windows.Forms.Button();
			this.txtMinValue = new System.Windows.Forms.TextBox();
			this.txtMaxValue = new System.Windows.Forms.TextBox();
			this.txtThreadNumber = new System.Windows.Forms.TextBox();
			this.txtOutput = new System.Windows.Forms.TextBox();
			this.lstMessage = new System.Windows.Forms.ListBox();
			this.lblBeginNumber = new System.Windows.Forms.Label();
			this.lblEndNumber = new System.Windows.Forms.Label();
			this.lblThreadNumber = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// btnCalculate
			// 
			this.btnCalculate.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnCalculate.Location = new System.Drawing.Point(64, 128);
			this.btnCalculate.Name = "btnCalculate";
			this.btnCalculate.Size = new System.Drawing.Size(75, 21);
			this.btnCalculate.TabIndex = 0;
			this.btnCalculate.Text = "开始计算";
			this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
			// 
			// txtMinValue
			// 
			this.txtMinValue.Location = new System.Drawing.Point(0, 24);
			this.txtMinValue.Name = "txtMinValue";
			this.txtMinValue.Size = new System.Drawing.Size(280, 21);
			this.txtMinValue.TabIndex = 1;
			this.txtMinValue.Text = "1";
			// 
			// txtMaxValue
			// 
			this.txtMaxValue.Location = new System.Drawing.Point(0, 72);
			this.txtMaxValue.Name = "txtMaxValue";
			this.txtMaxValue.Size = new System.Drawing.Size(280, 21);
			this.txtMaxValue.TabIndex = 2;
			this.txtMaxValue.Text = "10000";
			// 
			// txtThreadNumber
			// 
			this.txtThreadNumber.Location = new System.Drawing.Point(0, 128);
			this.txtThreadNumber.Name = "txtThreadNumber";
			this.txtThreadNumber.Size = new System.Drawing.Size(64, 21);
			this.txtThreadNumber.TabIndex = 3;
			this.txtThreadNumber.Text = "2";
			// 
			// txtOutput
			// 
			this.txtOutput.Location = new System.Drawing.Point(0, 184);
			this.txtOutput.Name = "txtOutput";
			this.txtOutput.Size = new System.Drawing.Size(280, 21);
			this.txtOutput.TabIndex = 4;
			this.txtOutput.Text = "0";
			// 
			// lstMessage
			// 
			this.lstMessage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lstMessage.ItemHeight = 12;
			this.lstMessage.Location = new System.Drawing.Point(0, 245);
			this.lstMessage.Name = "lstMessage";
			this.lstMessage.Size = new System.Drawing.Size(296, 136);
			this.lstMessage.TabIndex = 6;
			// 
			// lblBeginNumber
			// 
			this.lblBeginNumber.Location = new System.Drawing.Point(0, 0);
			this.lblBeginNumber.Name = "lblBeginNumber";
			this.lblBeginNumber.Size = new System.Drawing.Size(88, 23);
			this.lblBeginNumber.TabIndex = 7;
			this.lblBeginNumber.Text = "累加开始于:";
			this.lblBeginNumber.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblEndNumber
			// 
			this.lblEndNumber.Location = new System.Drawing.Point(0, 48);
			this.lblEndNumber.Name = "lblEndNumber";
			this.lblEndNumber.Size = new System.Drawing.Size(80, 23);
			this.lblEndNumber.TabIndex = 8;
			this.lblEndNumber.Text = "累加结束于:";
			this.lblEndNumber.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblThreadNumber
			// 
			this.lblThreadNumber.Location = new System.Drawing.Point(0, 104);
			this.lblThreadNumber.Name = "lblThreadNumber";
			this.lblThreadNumber.Size = new System.Drawing.Size(80, 23);
			this.lblThreadNumber.TabIndex = 9;
			this.lblThreadNumber.Text = "使用线程数:";
			this.lblThreadNumber.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(0, 160);
			this.label4.Name = "label4";
			this.label4.TabIndex = 10;
			this.label4.Text = "累加结果:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(0, 216);
			this.label5.Name = "label5";
			this.label5.TabIndex = 11;
			this.label5.Text = "信息:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// frmMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(296, 381);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.lblThreadNumber);
			this.Controls.Add(this.lblEndNumber);
			this.Controls.Add(this.lblBeginNumber);
			this.Controls.Add(this.lstMessage);
			this.Controls.Add(this.txtOutput);
			this.Controls.Add(this.txtThreadNumber);
			this.Controls.Add(this.txtMaxValue);
			this.Controls.Add(this.txtMinValue);
			this.Controls.Add(this.btnCalculate);
			this.Name = "frmMain";
			this.Text = "多线程测试";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmMain());
		}

	
		private void Add()
		{
			DateTime beginTime = DateTime.Now;

			long minValue = long.Parse(txtMinValue.Text);
			long maxValue = long.Parse(txtMaxValue.Text);
			int threadNumber = Convert.ToInt32(txtThreadNumber.Text);
			int threadOrder = Convert.ToInt32(Thread.CurrentThread.Name);

			long step = (maxValue - minValue + 1) / threadNumber;  
			long beginValue = minValue + step * (threadOrder -1);
			long endValue = beginValue + step;
			long result = 0;
			for(long i = beginValue; i < endValue; i ++)
			{
				result += i;
			}
			//lock(this)
			{
				long sum = long.Parse(txtOutput.Text);
				sum += result;
				txtOutput.Text = sum.ToString();
			}
			
			DateTime endTime = DateTime.Now;
			TimeSpan timeSpan = endTime - beginTime;

			string message = "线程" + Thread.CurrentThread.Name + ":" 
				+ beginValue.ToString() + "到" + endValue.ToString() 
				+ ",耗时:" + timeSpan.TotalMilliseconds.ToString() + "毫秒";
			this.lstMessage.Items.Add(message);
			Thread.CurrentThread.Abort();
		}

		private void btnCalculate_Click(object sender, System.EventArgs e)
		{
			txtOutput.Text = "0";
			lstMessage.Items.Clear();
			int threadNumber = Convert.ToInt32(this.txtThreadNumber.Text);
			for(int i = 1; i <= threadNumber; i ++)
			{
				ThreadStart threadStart = new ThreadStart(Add); 
				Thread thread = new Thread(threadStart);
				thread.Name = i.ToString();
				thread.Start();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -