`

C#如何进行多线程编程

 
阅读更多

由于多线程编程非常复杂,这个小例子只能算是一个入门线的知识点吧

首先建一个应用程序项目,命名为ThreadExample,在窗体上放一个文本框(textBox1),一个标签(lblResult),再放两个按钮,分别命名为btnStart、btnStop。

窗体代码:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->namespaceThreadExample
{
partial
classThreadExample
{
/**////<summary>
///Requireddesignervariable.
///</summary>
privateSystem.ComponentModel.IContainercomponents=null;

/**////<summary>
///Cleanupanyresourcesbeingused.
///</summary>
///<paramname="disposing">trueifmanagedresourcesshouldbedisposed;otherwise,false.</param>
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!=null))
{
components.Dispose();
}
base.Dispose(disposing);
}

WindowsFormDesignergeneratedcode
#regionWindowsFormDesignergeneratedcode

/**////<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.btnStart=newSystem.Windows.Forms.Button();
this.btnStop=newSystem.Windows.Forms.Button();
this.button1=newSystem.Windows.Forms.Button();
this.textBox1=newSystem.Windows.Forms.TextBox();
this.lblResult=newSystem.Windows.Forms.Label();
this.SuspendLayout();
//
//btnStart
//
this.btnStart.Location=newSystem.Drawing.Point(14,38);
this.btnStart.Name="btnStart";
this.btnStart.Size=newSystem.Drawing.Size(75,23);
this.btnStart.TabIndex=0;
this.btnStart.Text="启动";
this.btnStart.Click+=newSystem.EventHandler(this.btnStart_Click);
//
//btnStop
//
this.btnStop.Location=newSystem.Drawing.Point(14,68);
this.btnStop.Name="btnStop";
this.btnStop.Size=newSystem.Drawing.Size(75,23);
this.btnStop.TabIndex=1;
this.btnStop.Text="停止";
this.btnStop.Click+=newSystem.EventHandler(this.btnStop_Click);
//
//button1
//
this.button1.Location=newSystem.Drawing.Point(14,97);
this.button1.Name="button1";
this.button1.Size=newSystem.Drawing.Size(75,23);
this.button1.TabIndex=3;
this.button1.Text="关闭";
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
//
//textBox1
//
this.textBox1.Location=newSystem.Drawing.Point(14,11);
this.textBox1.Name="textBox1";
this.textBox1.Size=newSystem.Drawing.Size(75,21);
this.textBox1.TabIndex=4;
this.textBox1.Text="200";
//
//lblResult
//
this.lblResult.AutoSize=true;
this.lblResult.Location=newSystem.Drawing.Point(12,139);
this.lblResult.Name="lblResult";
this.lblResult.Size=newSystem.Drawing.Size(23,12);
this.lblResult.TabIndex=5;
this.lblResult.Text="0/0";
//
//ThreadExample
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(104,164);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnStart);
this.Name="ThreadExample";
this.Text="Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

privateSystem.Windows.Forms.ButtonbtnStart;
privateSystem.Windows.Forms.ButtonbtnStop;
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.TextBoxtextBox1;
privateSystem.Windows.Forms.LabellblResult;
}
}

程序代码:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Threading;

namespaceThreadExample
{
publicpartialclassThreadExample:Form
{
//声明一个线程
privateThreadtimerThread;
//声明一个变量,用来存储label值
intcount,i=0;

publicThreadExample()
{
InitializeComponent();
}

//把label的值加1;
publicvoidAddData()
{
//显示lable的值
if(i==count)
i
=0;
this.lblResult.Text=i.ToString()+"/"+count.ToString();
i
++;
}

//更新线程
publicvoidUpdataThread()
{
try
{
//在对控件的调用方法进行调用时,或需要一个简单委托又不想自己定义时可以使用该委托。
MethodInvokermi=newMethodInvoker(this.AddData);
while(true)
{
//在创建控件的基础句柄所在线程上异步执行指定的委托
this.BeginInvoke(mi);
Thread.Sleep(
50);
}
}
catch(ThreadInterruptedException)
{
//针对具体问题定制异常抛出显示
}
finally
{
//做一些处理
}
}

//启动线程
publicvoidStartThread()
{
StopThread();
timerThread
=newThread(newThreadStart(UpdataThread));
//获取或设置一个值,该值指示某个线程是否为后台线程。
timerThread.IsBackground=true;
timerThread.Start();
}

//停止线程
publicvoidStopThread()
{
if(timerThread!=null)
{
//中断线程
timerThread.Interrupt();
timerThread
=null;
}
}

//启动线程,显示结果
privatevoidbtnStart_Click(objectsender,EventArgse)
{
//调用线程启动函数
count=int.Parse(textBox1.Text);
this.StartThread();
}

//停止线程
privatevoidbtnStop_Click(objectsender,EventArgse)
{
//调用线程停止函数
this.StopThread();
}
}
}

编译后,运行,在文本框中输入200,点击开始按钮,标签为动态增长,点击停止可以暫停程序的执行。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics