C# 使用委托开始线程
【摘要】 使用委托的形式,调用线程,,,
using System;
using System.Threading;
namespace _012_线程
{ class Program { static void Main(string[] args) //在mian中线程是执行一个线程里面的语句的执行,是从上到下的 { //通过委托 开启一个线程 //===========...
使用委托的形式,调用线程,,,
using System;
using System.Threading;
namespace _012_线程
{ class Program { static void Main(string[] args) //在mian中线程是执行一个线程里面的语句的执行,是从上到下的 { //通过委托 开启一个线程 //==============可用泛型传参数(无返回值)============== Action threaA = ThreadTestA; threaA.BeginInvoke(null,null); //开启一个新的线程去执行,threaA所引用的方法 Action<int> threaB = ThreadTestB; threaB.BeginInvoke(111,null, null); //可以认为线程是同时执行的 (异步执行) Console.WriteLine("异步执行"); //================带返回值的形式==================== //第一种方式 检测线程结束 ----- IsCompleted线程是否行完毕 //Func<int, int> threaC = ThreadTestC; ////接收异步线程返回值 //IAsyncResult returnResult = threaC.BeginInvoke(111, null, null); //while (!res.IsCompleted) //{ // Console.Write("."); // Thread.Sleep(10); //控制子线程的检测频率,(每10ms检测一次) //} ////取得异步线程返回值 //int result = threaC.EndInvoke(res); //Console.WriteLine("IsCompleted方式检测:" + result); //第二种方式 检测线程结束 ----- 1000ms没结束就返回false,反之 Func<int, int> threaC = ThreadTestC; //接收异步线程返回值 IAsyncResult returnResult = threaC.BeginInvoke(111, null, null); bool isEnd = returnResult.AsyncWaitHandle.WaitOne(1000); int result = 0; if (isEnd) { result = threaC.EndInvoke(returnResult); } Console.WriteLine("EndInvoke()方式检测:" + isEnd +" "+ result); //第三种方式 检测线程结束 ----- 通过回调,检测线程结束 Func<int,string, string> threaD = ThreadTestD; //倒数第二个参数,表示委托类型的参数,(回调函数)当线程结束的时候会调用这个委托指向的方法 //最后一个参数,用来给回调函数传递数据 IAsyncResult asy = threaD.BeginInvoke(111,"Czhenya", OnCallKey, threaD); //改为Lamdba表达式 threaD.BeginInvoke(111, "Czhenya",(ar)=>{ string res = threaD.EndInvoke(ar); Console.WriteLine("在Lamdba表达式中取得:"+res); },null); Console.ReadKey(); } static void OnCallKey(IAsyncResult ar) { Func<int, string, string> thread = ar.AsyncState as Func<int, string, string>; string res = thread.EndInvoke(ar); Console.WriteLine("在回调函数中取到的结果 :"+res); } /// <summary> /// 一般是比较耗时的操作方法 /// </summary> static void ThreadTestA() { Console.WriteLine("ThreaTestA"); } static void ThreadTestB(int num) { Console.WriteLine("ThreaTestB "+num); } static int ThreadTestC(int num) { Console.WriteLine("ThreaTestC"); Thread.Sleep(100); //让当前线程休眠(暂停线程(参数单位:ms)) return num; } static string ThreadTestD(int num,string str) { Console.WriteLine("ThreaTestD"); return num +" "+ str; } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
运行结果图:
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/78225963
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)