一、簡單的通常線程異步
Func<int, int, int> Add = (a, b) => a + b; Thread t = new Thread(() => { Console.WriteLine(Add(3,4)); }); t.IsBackground = true; t.Start();
二、線程池運行線程函數
Func<int, int, int> Add = (a, b) => a + b; ThreadPool.QueueUserWorkItem(s => { Console.WriteLine(Add(4, 5)); });
三、異步spa
(1)簡單一點,在主線程中調用異步,而後在主線程中處理結果線程
Func<int, int, int> Add = (a, b) => a + b; var result = Add.BeginInvoke(3, 4, null, null); Console.WriteLine(Add.EndInvoke(result));
(2)在異步中執行結束後調用函數,並異步中執行處理結果。code
Func<int, int, int> Add = (a, b) => a + b; Add.BeginInvoke(3, 4, myCallBack, Add);
private void myCallBack(IAsyncResult ar) { var del = (Func<int, int, int>)ar.AsyncState; var res=del.EndInvoke(ar); Console.WriteLine(res); }