委托线程异步操作
核心代码:
TakesAWhileDelegate d1 = TakesAWhile;
IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);//异步操作
int result = d1.EndInvoke(ar);
完整代码
1 static int TakesAWhile(int data, int ms) 2 3 { 4 5 Console.WriteLine("线程开始"); 6 7 Thread.Sleep(ms); 8 9 Console.WriteLine("线程完成"); 10 11 return ++data; 12 13 } 14 15 16 public delegate int TakesAWhileDelegate(int data, int ms);//定义委托,参数2个,返回值类型int 17 18 19 static voidMain(string[] args) 20 21 { 22 23 TakesAWhileDelegate d1 = TakesAWhile; 24 25 IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);//异步操作 26 27 while (!ar.IsCompleted) 28 29 { 30 31 Console.Write("."); 32 33 Thread.Sleep(50); 34 35 } 36 37 int result = d1.EndInvoke(ar); 38 39 Console.WriteLine("result:{0}", result); 40 41 Console.Read(); 42 43 } 44 45
等待句柄
WaitOne()方法将一个超时时间作为可选的第一个参数。如果发生超时,WaitOne()方法就返回false。
1 TakesAWhileDelegate d1 = TakesAWhile; 2 3 IAsyncResult ar=d1.BeginInvoke(1,3000,null,null);//异步 4 5 while (true) 6 7 { 8 9 Console.Write("."); 10 11 if (ar.AsyncWaitHandle.WaitOne(50, false)) 12 13 { 14 15 Console.WriteLine("现在可以获取值"); 16 17 break; 18 19 } 20 21 } 22 23 int result = d1.EndInvoke(ar); 24 25 Console.WriteLine("result:{0}", result); 26 27 Console.Read();
异步回调
1 static void TakesAWhileCompleted(IAsyncResult ar) 2 3 { 4 5 if (ar == null) throw new ArgumentNullException("ar"); 6 7 8 TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate; 9 10 Trace.Assert(d1 != null, "对象类型"); 11 12 int result = d1.EndInvoke(ar); 13 14 Console.WriteLine("result:{0}",result); 15 16 } 17 18 TakesAWhileDelegate d1 = TakesAWhile; 19 20 d1.BeginInvoke(1, 3000, TakesAWhileCompleted, d1); 21 22 for (int i = 0; i < 100; i++) 23 24 { 25 26 Console.Write("."); 27 28 Thread.Sleep(50); 29 30 } 31 32 Console.Read();
Thread类
创建线程
Var t1=new Thread(Metho);
给线程传递参数
ParameterizedThreadStart委托
1 public struct Data 2 3 { 4 5 public string Message; 6 7 } 8 9 static void ThreadMainWithParameters(object o) 10 11 { 12 13 Data d = (Data)o; 14 15 Console.WriteLine("一个线程在运行,收到消息:{0}",d.Message); 16 17 Console.Read(); 18 19 } 20 21 public static void fun4() 22 23 { 24 25 var d = new Data { Message = "你好" }; 26 27 var t1 = new Thread(ThreadMainWithParameters); 28 29 t1.Start(d); 30 31 }
自定义类
public class MyThread { private string _data; public string Data { get { return _data; } set { _data = value; } } public MyThread(string data) { this._data = data; } public void ThreadMain() { Console.WriteLine("一个线程在运行,收到消息:{0}", _data); Console.Read(); } } #region 自定义类实现线程传递参数 public static void fun5() { var obj = new MyThread("你好"); var t3 = new Thread(obj.ThreadMain); t3.Start(); } #endregion
后台线程
默认情况下,用Thread类创建的线程是前台线程。线程池中的线程总是后台线程。
IsBackground=true;线程是后台线程,反之则为前台线程。
线程的优先级
属性Priority,值Highest,AboveNormal,BelowNormal,Lowest。
控制线程
Thread,Abort()停止另一个线程。
Thread.Join()等待线程的结束。
线程池
CurrentThread获取当前正在运行的线程
ManagedThreadId获取当前托管线程的唯一标识符
1 static void JobForAThread(object state) 2 3 { 4 5 for (int i = 0; i < 3; i++) 6 7 { 8 9 Console.WriteLine("loop{0},线程{1}", i, Thread.CurrentThread.ManagedThreadId); 10 11 Thread.Sleep(50); 12 13 } 14 15 } 16 17 #region 线程池 18 19 public static void fun6() 20 21 { 22 23 int nWorkerThreads; 24 25 int nComletionPortThreads; 26 27 ThreadPool.GetMaxThreads(out nWorkerThreads, out nComletionPortThreads); 28 29 Console.WriteLine("作业的最大线程数:{0},I/O的最大线程数:{1}", nWorkerThreads, nComletionPortThreads); 30 31 for (int i = 0; i < 5; i++) 32 33 { 34 35 ThreadPool.QueueUserWorkItem(JobForAThread);//线程进队列 36 37 } 38 39 Thread.Sleep(3000); 40 41 Console.Read(); 42 43 } 44 45 #endregion