由于多线程编程非常复杂,这个小例子只能算是一个入门线的知识点吧
首先建一个应用程序项目,命名为ThreadExample,在窗体上放一个文本框(textBox1),一个标签(lblResult),再放两个按钮,分别命名为btnStart、btnStop。
窗体代码:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->namespaceThreadExample
{
partialclassThreadExample
{
/**////<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,点击开始按钮,标签为动态增长,点击停止可以暫停程序的执行。
分享到:
相关推荐
C#多线程编程实例实战.doc
C#多线程编程实例实战.pdf
本书是一本通俗易懂的C#多线程编程指南,通过70多个容易理解的示例,循序渐进地讲解C#5.0中的异步及并发编程,引导读者了解Windows下C#多线程编程的多样性。 通过阅读本书,你将学到: 使用原始线程、异步线程,...
C#多线程编程实例实战C#多线程编程实例实战C#多线程编程实例实战
c#多线程编程实战(原书第二版)源码
C#多线程编程,系统地介绍了用C#进行多线程编程
Visual C#中的多线程编程 - c#(csharp).htmVisual C#中的多线程编程 - c#(csharp).htmVisual C#中的多线程编程 - c#(csharp).htmVisual C#中的多线程编程 - c#(csharp).htmVisual C#中的多线程编程 - c#(csharp)....
C#中Socket多线程编程实例.pdf
本文通过对常见的单个写入/多个阅读程序的分析来探索c#的多线程编程。
C#多线程编程实战原书第二版 完整实例源码,非常好的C#多线程编程学习资源,需要VS2015。
C#Socket多线程编程实例.pdf
C#Socket多线程编程实例,以C#为开发工具,实现了多线程的通信
C#Socket多线程编程实例[收集].pdf
C#多线程编程实战.pdf
为了创建这样的应用程序,你需要学习...本书的目的是给你提供C#中多线程以及并行编程的详尽指导。我们将从基本概念开始,每章主题比前一章都有所拔高,最后展示了现实世界中的并行编程模式以及Windows商店应用示例。
内有说明VS2008环境调试通过,无需再调试加载即可运行,里面关于异步调用的代码拷贝即可立即复用。希望对你有所帮助!这个是我苦战了三天才搞定的东西,也是我的总结资料在这里给大家一起分享!
WinForm C# 多线程编程并更新界面(UI)
C# Socket多线程编程实例-聊天程序
C#多线程编程实例 线程与窗体交互源码,该示例演示如何在线程安全的模式下调用Windows窗体上的控件。