📄 frmoperations.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace Example_5
{
/// <summary>
/// Summary description for frmOperations.
/// </summary>
public class frmOperations : System.Windows.Forms.Form
{
// Operations objOperations;
private System.Windows.Forms.Label lblSquare;
private System.Windows.Forms.Label lblIncrement;
private System.Windows.Forms.TextBox txtNumber;
private System.Windows.Forms.Button btnIncr;
private System.Windows.Forms.Button btnSquare;
private System.Windows.Forms.Button btnLoop;
private System.Windows.Forms.Label lblLoop;
private System.Windows.Forms.Label lblNumber;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private int _varIncrement;
private int _varSquare;
private int _varLoopValue;
private System.Threading.Thread objSquare;
private System.Threading.Thread objIncrement;
private System.Threading.Thread objLoop;
public frmOperations()
{
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblSquare = new System.Windows.Forms.Label();
this.lblIncrement = new System.Windows.Forms.Label();
this.txtNumber = new System.Windows.Forms.TextBox();
this.btnIncr = new System.Windows.Forms.Button();
this.btnSquare = new System.Windows.Forms.Button();
this.lblNumber = new System.Windows.Forms.Label();
this.btnLoop = new System.Windows.Forms.Button();
this.lblLoop = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblSquare
//
this.lblSquare.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblSquare.Location = new System.Drawing.Point(144, 48);
this.lblSquare.Name = "lblSquare";
this.lblSquare.Size = new System.Drawing.Size(184, 23);
this.lblSquare.TabIndex = 0;
//
// lblIncrement
//
this.lblIncrement.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblIncrement.Location = new System.Drawing.Point(144, 88);
this.lblIncrement.Name = "lblIncrement";
this.lblIncrement.Size = new System.Drawing.Size(184, 23);
this.lblIncrement.TabIndex = 1;
//
// txtNumber
//
this.txtNumber.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtNumber.Location = new System.Drawing.Point(136, 16);
this.txtNumber.Name = "txtNumber";
this.txtNumber.Size = new System.Drawing.Size(192, 21);
this.txtNumber.TabIndex = 3;
this.txtNumber.Text = "";
//
// btnIncr
//
this.btnIncr.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnIncr.Location = new System.Drawing.Point(120, 200);
this.btnIncr.Name = "btnIncr";
this.btnIncr.Size = new System.Drawing.Size(96, 23);
this.btnIncr.TabIndex = 4;
this.btnIncr.Text = "增加 9 (&I)";
this.btnIncr.Click += new System.EventHandler(this.btnIncr_Click);
//
// btnSquare
//
this.btnSquare.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnSquare.Location = new System.Drawing.Point(8, 200);
this.btnSquare.Name = "btnSquare";
this.btnSquare.Size = new System.Drawing.Size(96, 23);
this.btnSquare.TabIndex = 5;
this.btnSquare.Text = "平方(&S)";
this.btnSquare.Click += new System.EventHandler(this.btnSquare_Click);
//
// lblNumber
//
this.lblNumber.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblNumber.Location = new System.Drawing.Point(24, 16);
this.lblNumber.Name = "lblNumber";
this.lblNumber.Size = new System.Drawing.Size(104, 23);
this.lblNumber.TabIndex = 6;
this.lblNumber.Text = "输入一个数字:";
//
// btnLoop
//
this.btnLoop.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnLoop.Location = new System.Drawing.Point(232, 200);
this.btnLoop.Name = "btnLoop";
this.btnLoop.Size = new System.Drawing.Size(96, 24);
this.btnLoop.TabIndex = 7;
this.btnLoop.Text = "执行循环(&E)";
this.btnLoop.Click += new System.EventHandler(this.btnLoop_Click);
//
// lblLoop
//
this.lblLoop.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblLoop.Location = new System.Drawing.Point(144, 144);
this.lblLoop.Name = "lblLoop";
this.lblLoop.Size = new System.Drawing.Size(184, 23);
this.lblLoop.TabIndex = 8;
//
// frmOperations
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(336, 237);
this.Controls.Add(this.lblLoop);
this.Controls.Add(this.btnLoop);
this.Controls.Add(this.lblNumber);
this.Controls.Add(this.btnSquare);
this.Controls.Add(this.btnIncr);
this.Controls.Add(this.txtNumber);
this.Controls.Add(this.lblIncrement);
this.Controls.Add(this.lblSquare);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "frmOperations";
this.Text = "操作";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmOperations());
}
private void btnSquare_Click(object sender, System.EventArgs e)
{
_varSquare = int.Parse(txtNumber.Text);
// 在计算完成之前,禁用 btnSquare。
btnSquare.Enabled = false;
ChooseThreads(1);
}
private void btnIncr_Click(object sender, System.EventArgs e)
{
_varIncrement = int.Parse(txtNumber.Text);
// 在计算完成之前,禁用 btnIncr。
btnIncr.Enabled = false;
ChooseThreads(2);
}
private void btnLoop_Click(object sender, System.EventArgs e)
{
_varLoopValue = int.Parse(txtNumber.Text);
// 在计算完成之前,禁用 btnLoop。
btnLoop.Enabled = false;
ChooseThreads(3);
}
// 该方法将计算给定数字的平方。
public void Square()
{
double result;
Console.WriteLine ("运行平方线程");
result = _varSquare * _varSquare;
lblSquare.Text = "平方是:" + result.ToString();
// 重新启用按钮以再次使用它。
btnSquare.Enabled = true;
}
// 该方法将给定的数字增加 9。
public void Increment()
{
int result = _varIncrement + 9;
Console.WriteLine ("运行增加线程");
lblIncrement.Text = "增加后的值是:"+result.ToString();
// 重新启用按钮以再次使用它。
btnIncr.Enabled = true;
}
// 该方法将运行
// 含有嵌套循环的循环 _varLoopValue 次。
public void RunALoop()
{
int index;
Console.WriteLine ("循环");
for (index = 1; index <= _varLoopValue; index++)
{
Console.WriteLine("运行循环");
//给定延迟的毫秒数
Thread.Sleep(500);
}
btnLoop.Enabled = true;
lblLoop.Text = "循环已执行" + _varLoopValue.ToString() +"次";
}
public void ChooseThreads(int threadNumber)
{
// 根据收到的值确定要开始的线程。
switch(threadNumber)
{
case 1:
// 设置线程使用子程序的地址以便
// 线程从该地址开始运行。
objSquare = new System.Threading.Thread(new
System.Threading.ThreadStart(this.Square));
// 开始线程。
objSquare.Start();
break;
case 2:
objIncrement = new System.Threading.Thread(new
System.Threading.ThreadStart(this.Increment));
objIncrement.Start();
break;
case 3:
objLoop = new System.Threading.Thread(new
System.Threading.ThreadStart(this.RunALoop));
objLoop.Start();
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -