1 创建多线程,一般情况有以下几种:(1)通过Thread类 (2)通过Delegate.BeginInvoke方法 (3)线程池
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers; using System.Threading; using System.IO; using System.Xml; namespace XMLTest { public class XmlTest { delegate void Delegate_Test(int a); static void Main(string[] args) { //通过Thread类,其实也是通过ThreadStart委托 Thread t1 = new Thread(new ThreadStart(fun1)); t1.Start(); // 通过委托,然后使用BeginInvoke方法 Delegate_Test dt = new Delegate_Test(fun2); dt.BeginInvoke(10,null,null); //线程池,使用WaitCallback ThreadPool.QueueUserWorkItem(new WaitCallback(fun3),3); Console.Read(); } public static void fun1() { while(1>0) { Console.WriteLine("来自普通Thread"); Thread.Sleep(800); } } public static void fun2(int a) { while(a>0) { Console.WriteLine("来自beginInvoke"); Thread.Sleep(1500); } } static void fun3(Object o) { while (1> 0) { Console.WriteLine("来自ThreadPool"); Thread.Sleep(2050); } } } }
结果: