using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WinFormsApp_CreateAThread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 方式2-1 启动线程(带输入参数的)
private void btn1_Click(object sender, EventArgs e)
{
MyObject obj = new MyObject(1, 2);//或者使用全局变量代替obj
Thread thread = new Thread(obj.FunctionName);
thread.Start();
}
public class MyObject
{
public int a1, b1;
public MyObject(int a, int b)
{
a1 = a;
b1 = b;
}
public void FunctionName()
{
MessageBox.Show("a:" + a1 + ",b:" + b1);
}
}
#endregion
#region 方式2-2 启动线程(带输入参数的)
private void btn2_Click(object sender, EventArgs e)
{
Thread thread = new Thread(myStaticParamThreadMethod);
thread.Start("通过委托的参数传值");
}
public static void myStaticParamThreadMethod(Object obj)
{
MessageBox.Show(obj.ToString());
}
//[ComVisibleAttribute(false)]
//public delegate void ParameterizedThreadStart(Object obj);
//这个Thread类的构造方法的定义如下:
//public Thread(ParameterizedThreadStart start);
#endregion
#region 方式3 线程池技术
RegisteredWaitHandle RW;
private void btn3_Click(object sender, EventArgs e)
{
//ThreadPool.QueueUserWorkItem(ThreadProc);//类似于new Thread
RW = ThreadPool.RegisterWaitForSingleObject(wait, new WaitOrTimerCallback(test), state, 1000, false);
wait.Set();
}
AutoResetEvent wait = new AutoResetEvent(false);
object state = new object();
int count = 0;
/// <summary>
/// 会定时反复调用
/// </summary>
private void test(object state, bool timedOut)
{
SetText(DateTime.Now.ToString());
count++;
if (count > 5) RW.Unregister(wait);//执行5次后,退出
}
/// <summary>
/// 调用UI线程,修改界面数据
/// </summary>
public void SetText(string argText)
{
if (lblResult.InvokeRequired)
{
lblResult.Invoke(new SetTextDelegateMethod(SetText), new[] { argText });
}
else
lblResult.Text = argText;
}
public delegate void SetTextDelegateMethod(string argText);
//void ThreadProc(Object stateInfo)
//{
// MessageBox.Show("Hello from the thread pool.");
//}
#endregion
}
}
namespace WinFormsApp_CreateAThread
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn1 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.lblResult = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(24, 99);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(141, 44);
this.btn1.TabIndex = 0;
this.btn1.Text = "创建线程并带参数传值1";
this.btn1.UseVisualStyleBackColor = true;
this.btn1.Click += new System.EventHandler(this.btn1_Click);
//
// btn2
//
this.btn2.Location = new System.Drawing.Point(24, 174);
this.btn2.Name = "btn2";
this.btn2.Size = new System.Drawing.Size(141, 39);
this.btn2.TabIndex = 1;
this.btn2.Text = "创建线程并带参数传值2";
this.btn2.UseVisualStyleBackColor = true;
this.btn2.Click += new System.EventHandler(this.btn2_Click);
//
// btn3
//
this.btn3.Location = new System.Drawing.Point(211, 174);
this.btn3.Name = "btn3";
this.btn3.Size = new System.Drawing.Size(131, 39);
this.btn3.TabIndex = 2;
this.btn3.Text = "通过线程池创建线程3";
this.btn3.UseVisualStyleBackColor = true;
this.btn3.Click += new System.EventHandler(this.btn3_Click);
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Location = new System.Drawing.Point(236, 115);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(41, 12);
this.lblResult.TabIndex = 3;
this.lblResult.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(399, 339);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.btn3);
this.Controls.Add(this.btn2);
this.Controls.Add(this.btn1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Label lblResult;
}
}
创建线程的几种方式(代码示例),布布扣,bubuko.com
原文:http://www.cnblogs.com/jx270/p/3583614.html