C#线程系列讲座(2):Thread 类的应用
本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:C#线程系列讲座(1):BeginInvoke 和 EndInvoke 方法
一、 Thread 类的基本用法
通过 System.Threading.Thread 类可以开始新的线程,并在线程堆栈中运行静态或实例
方法。可以通过 Thread 类的的构造方法传递一个无参数,并且不返回值(返回 void)的委托
(ThreadStart),这个委托的定义如下:
[ComVisibleAttribute(true)]
public delegate void ThreadStart()
我们可以通过如下的方法来建立并运行一个线程。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyThread
{
class Program
{
public static void myStaticThreadMethod()
{
Console.WriteLine("myStaticThreadMethod");
}
static void Main(string[] args)
{
Thread thread1 = new Thread(myStaticThreadMethod);