初次接觸c#時做的一個小例子,先貼到這吧。由于多線程編程非常復雜,這個小例子只能算是一個入門線的知識點吧
首先建一個應用程序項目,命名為threadexample,在窗體上放一個文本框(textbox1) ,一個標簽(lblresult),再放兩個按鈕,分別命名為btnstart、btnstop。
窗體代碼:
namespace threadexample
...{
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 = "關閉";
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;
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
...{
//在對控件的調用方法進行調用時,或需要一個簡單委托又不想自己定義時可以使用該委托。
methodinvoker mi = new methodinvoker(this.adddata);
while (true)
...{
//在創建控件的基礎句柄所在線程上異步執行指定的委托
this.begininvoke(mi);
thread.sleep(50);
}
}
catch (threadinterruptedexception)
...{
//針對具體問題定制異常拋出顯示
}
finally
...{
//做一些處理
}
}
//啟動線程
public void startthread()
...{
stopthread();
timerthread = new thread(new threadstart(updatathread));
//獲取或設置一個值,該值指示某個線程是否為后臺線程。
timerthread.isbackground = true;
timerthread.start();
}
//停止線程
public void stopthread()
...{
if (timerthread != null)
...{
//中斷線程
timerthread.interrupt();
timerthread = null;
}
}
//啟動線程,顯示結果
private void btnstart_click(object sender, eventargs e)
...{
//調用線程啟動函數
count = int.parse(textbox1.text);
this.startthread();
}
//停止線程
private void btnstop_click(object sender, eventargs e)
...{
//調用線程停止函數
this.stopthread();
}
}
}
編譯后,運行,在文本框中輸入200,點擊開始按鈕,標簽為動態增長,點擊停止可以暫停程序的執行。
新聞熱點
疑難解答