1. 一句話理解異步異步
我叫你去吃飯,叫完你不去,那我就會一直等你,直到你和我一塊兒去吃飯。這叫同步!async
我叫你去吃飯,叫完無論你去不去,我都不會等你,我本身去吃飯。這叫異步!函數
2. 異步使用spa
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0}", Thread.CurrentThread.ManagedThreadId,t); }; action.BeginInvoke("異步參數",null,null); Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
3. 異步回調線程
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0}", Thread.CurrentThread.ManagedThreadId,t); }; AsyncCallback callback = t => { Console.WriteLine("這裏是回調函數 當前線程是{0}", Thread.CurrentThread.ManagedThreadId); }; action.BeginInvoke("異步參數", callback, null); Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
4. 異步回調帶參數code
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0}", Thread.CurrentThread.ManagedThreadId,t); }; AsyncCallback callback = t => { Console.WriteLine(t.AsyncState); Console.WriteLine("這裏是回調函數 當前線程是{0}", Thread.CurrentThread.ManagedThreadId); }; action.BeginInvoke("異步參數", callback, "無名小蝦"); Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
5. 異步等待blog
第一種方式:IsCompleted回調函數
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0}", Thread.CurrentThread.ManagedThreadId,t); }; AsyncCallback callback = t => { Console.WriteLine(t.AsyncState); Console.WriteLine("這裏是回調函數 當前線程是{0}", Thread.CurrentThread.ManagedThreadId); }; IAsyncResult asyncResult = action.BeginInvoke("異步參數", callback, "無名小蝦"); while (!asyncResult.IsCompleted) { Console.WriteLine("等待中......"); Thread.Sleep(200); } Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
第二種方式:WaitOne()同步
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0}", Thread.CurrentThread.ManagedThreadId,t); }; AsyncCallback callback = t => { Console.WriteLine(t.AsyncState); Console.WriteLine("這裏是回調函數 當前線程是{0}", Thread.CurrentThread.ManagedThreadId); }; IAsyncResult asyncResult = action.BeginInvoke("異步參數", callback, "無名小蝦"); asyncResult.AsyncWaitHandle.WaitOne(); Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
第三種方式:EndInvoke()string
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0}", Thread.CurrentThread.ManagedThreadId,t); }; AsyncCallback callback = t => { Console.WriteLine(t.AsyncState); Console.WriteLine("這裏是回調函數 當前線程是{0}", Thread.CurrentThread.ManagedThreadId); }; IAsyncResult asyncResult = action.BeginInvoke("異步參數", callback, "無名小蝦"); action.EndInvoke(asyncResult); Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
6. 異步返回值
主線程獲取到返回值
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Func<string,string> func = t => { string result = "這是個人返回值"; for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0},返回的參數是 {2}", Thread.CurrentThread.ManagedThreadId,t, result); return result; }; IAsyncResult asyncResult = func.BeginInvoke("異步參數", null, null); string res = func.EndInvoke(asyncResult); Console.WriteLine("獲取到返回值:{0}", res); Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
回調獲取到返回值
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Func<string,string> func = t => { string result = "這是個人返回值"; for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前參數是{1},當前線程是{0},返回的參數是 {2}", Thread.CurrentThread.ManagedThreadId,t, result); return result; }; AsyncCallback asyncCallback = t => { string res = func.EndInvoke(t); Console.WriteLine("獲取到返回值:{0}", res); }; func.BeginInvoke("異步參數", asyncCallback, null); Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }