初次接触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,点击开始按钮,标签为动态增长,点击停止可以暫停程序的执行。
新闻热点
疑难解答