C#之线程
今天給大家分享關于C#的幾種調用的方法。
?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;namespace _006線程池 {class Program{static void Main(string[] args){// Thread t1 = new Thread(ThreadMain) { IsBackground = false };//為true的時候就需要結束該線程,不管你是否結束,false的時候是一個前臺線程,他會等你結束后在結束// t1.Abort();//終止線程,拋出異常// t1.Start();// t1.Join();//阻止線程運行,知道實例線程結束//Console.WriteLine("Thread +" + Thread.CurrentThread.ManagedThreadId + " started");//Console.WriteLine("Main thread ending now.");ThreadPool.QueueUserWorkItem(ThreadMain);//線程池預留空線程等待發配任務,而發配任務的時候,隨機給完成任務的線程重新發配任務ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);ThreadPool.QueueUserWorkItem(ThreadMain);Console.ReadKey();}static void ThreadMain(object state){Console.WriteLine("Thread +" + Thread.CurrentThread.ManagedThreadId + " started");Thread.Sleep(1000);} } }-------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;namespace _007線程_任務開啟 {class Program{static void Main(string[] args){//第一種方式//Task t = new Task(ThreadMain);//傳遞一個線程執行一個方法//t.Start();//用任務開啟一個線程//第二種方式開啟的方式TaskFactory task = new TaskFactory();Task t= task.StartNew(ThreadMain);Console.ReadKey();}static void ThreadMain(){Console.WriteLine("Thread +" + Thread.CurrentThread.ManagedThreadId + " started");Thread.Sleep(1000);}} } -------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;namespace _004線程_委托發起的線程 {class Program{static void Main(string[] args)//開啟一個線程,從上到下的執行這些內容,其實就是一個進程{//第一種開啟線程的方式//Func<int,string,int> a = Text;a.BeginInvoke(15,"萌萌",null,null);//開啟一個線程,執行a所引用的方法,需要一些時間去操作,他的線程與main的線程是異步操作//IAsyncResult ar= a.BeginInvoke(15, "萌萌", null, null);//他的返回值是當前異步線程的狀態,return 的返回值是在線程結束時返回的//while (ar.IsCompleted==false)//{// Thread.Sleep(10);// Console.WriteLine("*");//}//int res = a.EndInvoke(ar);//在線程結束時拿到返回值100//Console.WriteLine(res);//Console.WriteLine("main");//Console.ReadKey();//運行新的線程的時候必須結束暫停//第二種開啟線程的方式//Func<int, string, int> a = Text;//IAsyncResult ar = a.BeginInvoke(15, "萌萌", null, null);//判斷線程運行的狀態//bool IsEnd= ar.AsyncWaitHandle.WaitOne(1000);//等待線程結束時間間隔//if (IsEnd)//{// int res = a.EndInvoke(ar);// Console.WriteLine(res);//}//第三種發起線程的方式Func<int, string, int> a = Text;/* IAsyncResult ar= a.BeginInvoke(15, "萌萌", BackOut, a);/*///倒數第二個參數是回調函數,倒數第一個參數是委托對象(BackOut回調函數是在線程結束時調用)a.BeginInvoke(15, "萌萌", ar =>//用lambad表達式去檢測結束一個線程{int res = a.EndInvoke(ar);Console.WriteLine(res);}, a);Console.ReadKey();}static void BackOut(IAsyncResult ar)//傳遞線程狀態{Func<int, string, int> a = ar.AsyncState as Func<int, string, int>;int res = a.EndInvoke(ar);Console.WriteLine(res);}static int Text(int id, string name){Console.WriteLine("測試線程" + id + name);Thread.Sleep(200);//單位為ms,線程睡眠0.2sreturn 100;}} }
轉載于:https://www.cnblogs.com/shuanglu/p/8297561.html
總結
- 上一篇: React路上遇到的Bug
- 下一篇: 关于JAVA编译时找不到自定义包的问题