public static void PostAsync(int a, int b) { AsyncSum caller = MySum; //FooCallBack,caller为额外的参数 caller.BeginInvoke(a, b, FooCallBack, caller); //第三个参数为回调 }
static void Main(string[] args) { AsyncSum asum = Sum; asum.BeginInvoke(100, (IAsyncResult ar) => { Console.WriteLine(asum.EndInvoke(ar)); }, null); Console.ReadKey(); }
static void Main(string[] args) { // Ініціалізація byte[][] b = new byte[10000][]; for (int i = 0; i < b.Length; i++) { b[i] = new byte[10000]; for (int j = 0; j < b[i].Length; j++) { b[i][j] = 1; } } int[] async_threadId = new int[b.Length]; for (int i = 0; i < b.Length; i++) { async_threadId[i] = i; } List <AsyncMethodCaller> async_caller = new List <AsyncMethodCaller>(); List <IAsyncResult> async_res = new List <IAsyncResult>(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // Проходи масив по рядку for (int i = 0; i < b.Length; i++) { AsyncSum async_sum = new AsyncSum(); AsyncMethodCaller caller = new AsyncMethodCaller(async_sum.TestMethod); async_caller.Add(caller); async_res.Add(async_caller[i].BeginInvoke(b[i], out async_threadId[i], null, null)); } int returnValue; for (int i = 0; i < b.Length; i++) { async_res[i].AsyncWaitHandle.WaitOne(); returnValue = async_caller[i].EndInvoke(out async_threadId[i], async_res[i]); async_res[i].AsyncWaitHandle.Close(); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", async_threadId[i], returnValue); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); Console.ReadLine(); }
static void Main(string[] args) { // Ініціалізація byte[][] b = new byte[10000][]; for (int i = 0; i < b.Length; i++) { b[i] = new byte[10000]; for (int j = 0; j < b[i].Length; j++) { b[i][j] = 1; } } int[] async_threadId = new int[b.Length]; for (int i = 0; i < b.Length; i++) { async_threadId[i] = i; } List<AsyncMethodCaller> async_caller = new List<AsyncMethodCaller>(); List<IAsyncResult> async_res = new List<IAsyncResult>(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // Проходи масив по рядку for (int i = 0; i < b.Length; i++) { AsyncSum async_sum = new AsyncSum(); AsyncMethodCaller caller = new AsyncMethodCaller(async_sum.TestMethod); async_caller.Add(caller); async_res.Add (async_caller[i].BeginInvoke( b[i], out async_threadId[i], null, null) ); } int returnValue; for (int i = 0; i < b.Length; i++) { async_res[i].AsyncWaitHandle.WaitOne(); returnValue = async_caller[i].EndInvoke(out async_threadId[i], async_res[i]); async_res[i].AsyncWaitHandle.Close(); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", async_threadId[i], returnValue); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); Console.ReadLine(); }
public void Cleanup() { tokenSource.Dispose(); asyncSum = null; }
public void Initialize() { tokenSource = new CancellationTokenSource(); asyncSum = new AsyncSum(); }
private static void FooCallBack(IAsyncResult ar) { AsyncSum caller = (AsyncSum)ar.AsyncState;; //获得BeginInvoke第4个参数 int number = caller.EndInvoke(ar); //获取运算的结果 }