📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading ;
namespace UseThreadApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ToolBarButton toolBarButton4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
this.SuspendLayout();
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3,
this.toolBarButton4});
this.toolBar1.DropDownArrows = true;
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(304, 38);
this.toolBar1.TabIndex = 1;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.Text = "Start";
//
// toolBarButton2
//
this.toolBarButton2.Text = "Sleep";
//
// toolBarButton3
//
this.toolBarButton3.Text = "Suspend";
//
// toolBarButton4
//
this.toolBarButton4.Text = "Abort";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 157);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.toolBar1});
this.Name = "Form1";
this.Text = "UseThreadingApp";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private Thread calcThread; //声明一个线程
private Form2 calcForm; //一个Form2窗体
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (toolBar1.Buttons .IndexOf (e.Button ))
{
case 0:
if (calcThread==null)
{
calcForm.Show ();
//把线程实例化,并设置优先级
calcThread = new Thread (new ThreadStart (calcForm.MyCalculate));
calcThread.Priority = ThreadPriority.Normal;
//启动线程
calcThread.Start ();
}
break;
case 1:
//当前线程休眠10秒
Thread.Sleep (10*1000);
break;
case 2:
if (calcThread.ThreadState == ThreadState.Running )
{ //挂起线程
calcThread.Suspend ();
e.Button .Text = "Resume";
}
else if (calcThread.ThreadState == ThreadState.Suspended )
{ //继续线程
calcThread.Resume ();
e.Button .Text = "Suspend";
}
break;
case 3:
if (calcThread.IsAlive )
{ //终止线程
calcThread.Abort ();
calcForm.Hide ();
}
break;
}
}
private void Form1_Load(object sender, System.EventArgs e)
{ //初始化Form2实例,并隐藏起来
calcForm = new Form2 ();
calcForm.Hide ();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -