在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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); }
} } }
|
请发表评论