private void button17_Click(object sender, EventArgs e) { Console.WriteLine("++++++++++++++++++++++++++++++++++++++btn_Async_Click {0} 开始", Thread.CurrentThread.ManagedThreadId); IAsyncResult asyncResult = null; doSomeMethodDelegate methodDelegate = new doSomeMethodDelegate(DoSomeThing); AsyncCallback asyncCallback = x => { Console.WriteLine("AsyncState=" + x.AsyncState); Console.WriteLine("asyncCallback"); Console.WriteLine("asyncResult==asyncCallback?" + x.Equals(asyncResult)); }; asyncResult = methodDelegate.BeginInvoke("复习", asyncCallback, "@object"); int i = 0; while (!asyncResult.IsCompleted) { Console.WriteLine("+++++++++++正在计算中!++++++++++ + 已经完成 {0}", 10 * i++); Thread.Sleep(100); } Console.WriteLine("++++++++++++++++++++++++++++++++++++++btn_Async_Click {0} 结束", Thread.CurrentThread.ManagedThreadId); }
private void btn_Async_Click(object sender, EventArgs e) { Console.WriteLine("++++++++++++++++++++++++++++++++++++++btn_Async_Click {0} 开始", Thread.CurrentThread.ManagedThreadId); doSomeMethodDelegate doSomeMethod = new doSomeMethodDelegate(DoSomeThing); IAsyncResult asyncResult = null; AsyncCallback asyncCallback = b => { Console.WriteLine(asyncResult.Equals(b)); Console.WriteLine(b.AsyncState); Console.WriteLine("回调函数!"); }; asyncResult = doSomeMethod.BeginInvoke("btn_Async_Click", asyncCallback, "abcdefg"); asyncResult = doSomeMethod.BeginInvoke("btn_Async_Click", xx => { Console.WriteLine(xx.AsyncState); Console.WriteLine("回调函数!"); }, "abcdefg"); int i = 1; while (!asyncResult.IsCompleted) { Console.WriteLine("+++++++++++正在计算中!++++++++++ + 已经完成 {0}", 10 * i++); Thread.Sleep(100); } asyncResult.AsyncWaitHandle.WaitOne(); asyncResult.AsyncWaitHandle.WaitOne(100); asyncResult.AsyncWaitHandle.WaitOne(1000); doSomeMethod.EndInvoke(asyncResult); Func <int, string> func = z => { DoSomeThing("btn_Async_Click"); return("2017----"); }; asyncResult = func.BeginInvoke(DateTime.Now.Millisecond, a => { Console.WriteLine(a.AsyncState); Console.WriteLine("这里是回调函数 {0}", Thread.CurrentThread.ManagedThreadId); }, "AlwaysOnline"); string sresult = func.EndInvoke(asyncResult); //ThreadPool.QueueUserWorkItem(x => { }); //ManualResetEvent manualResetEvent = new ManualResetEvent(false); //manualResetEvent.Set(); //manualResetEvent.Reset(); TaskFactory taskFactory = new TaskFactory(); taskFactory.ContinueWhenAll(new Task[3], x0 => { }); Action <string> action = x3 => { Console.WriteLine("{0}", x3); }; Action <string> action1 = x1 => DoSomeThing("a"); Func <string, int> funccc = x2 => { Console.WriteLine("{0}", x2); return(3); }; int x = funccc("ab"); Parallel.Invoke(); Parallel.ForEach(new string[] { "a" }, (y, state) => { Console.Write(y); state.Break(); }); ParallelOptions parallelOptions = new ParallelOptions(); parallelOptions.MaxDegreeOfParallelism = 5; //Parallel.For<string>() CancellationTokenSource cts = new CancellationTokenSource(); //cts.Token; cts.Cancel(); Console.WriteLine("++++++++++++++++++++++++++++++++++++++btn_Async_Click {0} 结束", Thread.CurrentThread.ManagedThreadId); }