C#如何進行多線程編程
由于多線程編程非常復(fù)雜,這個小例子只能算是一個入門線的知識點吧
首先建一個應(yīng)用程序項目,命名為ThreadExample,在窗體上放一個文本框(textBox1) ,一個標(biāo)簽(lblResult),再放兩個按鈕,分別命名為btnStart、btnStop。
窗體代碼:
{
partial class ThreadExample
{
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows Form Designer generated code#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.btnStart = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.lblResult = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(14, 38);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "啟動";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(14, 68);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 23);
this.btnStop.TabIndex = 1;
this.btnStop.Text = "停止";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(14, 97);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "關(guān)閉";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(14, 11);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(75, 21);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "200";
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Location = new System.Drawing.Point(12, 139);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(23, 12);
this.lblResult.TabIndex = 5;
this.lblResult.Text = "0/0";
//
// ThreadExample
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.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
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label lblResult;
}
}
程序代碼:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ThreadExample
{
public partial class ThreadExample : Form
{
//聲明一個線程
private Thread timerThread;
//聲明一個變量,用來存儲label值
int count, i = 0;
public ThreadExample()
{
InitializeComponent();
}
//把label的值加1;
public void AddData()
{
//顯示lable的值
if (i == count)
i = 0;
this.lblResult.Text = i.ToString() + "/" + count.ToString();
i++;
}
//更新線程
public void UpdataThread()
{
try
{
//在對控件的調(diào)用方法進行調(diào)用時,或需要一個簡單委托又不想自己定義時可以使用該委托。
MethodInvoker mi = new MethodInvoker(this.AddData);
while (true)
{
//在創(chuàng)建控件的基礎(chǔ)句柄所在線程上異步執(zhí)行指定的委托
this.BeginInvoke(mi);
Thread.Sleep(50);
}
}
catch (ThreadInterruptedException)
{
//針對具體問題定制異常拋出顯示
}
finally
{
//做一些處理
}
}
//啟動線程
public void StartThread()
{
StopThread();
timerThread = new Thread(new ThreadStart(UpdataThread));
//獲取或設(shè)置一個值,該值指示某個線程是否為后臺線程。
timerThread.IsBackground = true;
timerThread.Start();
}
//停止線程
public void StopThread()
{
if (timerThread != null)
{
//中斷線程
timerThread.Interrupt();
timerThread = null;
}
}
//啟動線程,顯示結(jié)果
private void btnStart_Click(object sender, EventArgs e)
{
//調(diào)用線程啟動函數(shù)
count = int.Parse(textBox1.Text);
this.StartThread();
}
//停止線程
private void btnStop_Click(object sender, EventArgs e)
{
//調(diào)用線程停止函數(shù)
this.StopThread();
}
}
}
編譯后,運行,在文本框中輸入200,點擊開始按鈕,標(biāo)簽為動態(tài)增長,點擊停止可以暫停程序的執(zhí)行。
作者:
RDIF
出處:
http://www.rzrgm.cn/huyong/
Email:
406590790@qq.com
QQ:
406590790
微信:
13005007127(同手機號)
框架官網(wǎng):
http://www.guosisoft.com/
http://www.rdiframework.net/
框架其他博客:
http://blog.csdn.net/chinahuyong
http://www.rzrgm.cn/huyong
國思RDIF開發(fā)框架
,
給用戶和開發(fā)者最佳的.Net框架平臺方案,為企業(yè)快速構(gòu)建跨平臺、企業(yè)級的應(yīng)用提供強大支持。
關(guān)于作者:系統(tǒng)架構(gòu)師、信息系統(tǒng)項目管理師、DBA。專注于微軟平臺項目架構(gòu)、管理和企業(yè)解決方案,多年項目開發(fā)與管理經(jīng)驗,曾多次組織并開發(fā)多個大型項目,在面向?qū)ο蟆⒚嫦蚍?wù)以及數(shù)據(jù)庫領(lǐng)域有一定的造詣?,F(xiàn)主要從事基于
RDIF
框架的技術(shù)開發(fā)、咨詢工作,主要服務(wù)于金融、醫(yī)療衛(wèi)生、鐵路、電信、物流、物聯(lián)網(wǎng)、制造、零售等行業(yè)。
如有問題或建議,請多多賜教!
本文版權(quán)歸作者和CNBLOGS博客共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,可以通過微信、郵箱、QQ等聯(lián)系我,非常感謝。

浙公網(wǎng)安備 33010602011771號