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
{
delegatevoid Delegate_Test(int a);
staticvoid 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();
}
publicstaticvoid fun1()
{
while(1>0)
{
Console.WriteLine("来自普通Thread");
Thread.Sleep(800);
}
}
publicstaticvoid fun2(int a)
{
while(a>0)
{
Console.WriteLine("来自beginInvoke");
Thread.Sleep(1500);
}
}
staticvoid fun3(Object o)
{
while (1> 0)
{
Console.WriteLine("来自ThreadPool");
Thread.Sleep(2050);
}
}
}
}