一、异步委托开启线程
public static void Main(string[] args){
Action<int,int> a=add;
a.BeginInvoke(3,4,null,null);//前两个是add方法的参数,后两个可以为空
Console.WriteLine("main()");
Console.ReadKey();
}
static void add(int a,int b){
Console.WriteLine(a+b);
}
运行结果:

如果不是开启线程,像平常一样调用的话,应该先输出7,再输出main()
二、通过thread类开启线程
using System;
using System.Threading;
public static void Main(string[] args){
Thread t=new Thread(DownLoadFile_My);//创建了线程还未开启
t.Start("http://www.easck.com//thread开启线程要求:该方法参数只能有一个,且是object类型
static void DownLoadFile_My(object filePath){
Console.WriteLine("开始下载:"+filePath);
Thread.Sleep(2000);
Console.WriteLine("下载完成!");
}
运行结果:

三、通过线程池开启线程
public static void Main(string[] args){
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
ThreadPool.QueueUserWorkItem(DownLoadFile_My);
Console.WriteLine("main()");
Console.ReadKey();
}
static void DownLoadFile_My(object state){
Console.WriteLine("开始下载... 线程ID:"+Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(2000);
Console.WriteLine("下载完成!");
}










