コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("这是主线程 ID:" + Thread.CurrentThread.ManagedThreadId);
            //无参无返回值
            DoSomething doSomething = Method1;

            doSomething.BeginInvoke(null, null);

            //无参有返回值
            DoSomethingReturn doSomethingReturn = Method2;
            IAsyncResult      iasyncResult      = doSomethingReturn.BeginInvoke(null, null);
            int result = doSomethingReturn.EndInvoke(iasyncResult);//要获取委托的返回值,必须等待子线程执行结束

            //有参无返回值
            DoMore doMore = Method3;

            doMore.BeginInvoke(16, "guo", null, null);

            //有参有返回值
            DoMoreReturn doMoreReturn  = Method4;
            IAsyncResult iasyncResult2 = doMoreReturn.BeginInvoke(16, "guo", null, null);
            int          result2       = doMoreReturn.EndInvoke(iasyncResult2);//要获取委托的返回值,必须等待子线程执行结束

            //异步回调
            DoMoreReturn doMoreReturn2 = Method4;

            doMoreReturn2.BeginInvoke(16, "guo", Callback, "wulala");
            Console.ReadKey();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: lxy-l/Test
        private static void Callback(IAsyncResult iasyncResult)
        {
            Console.WriteLine("Callback-Start【ThreadId=" + Thread.CurrentThread.ManagedThreadId + "】:" + DateTime.Now);
            AsyncResult asyncResult = (AsyncResult)iasyncResult;
            //获取回调方法的参数
            string parameter = asyncResult.AsyncState.ToString();
            //获取委托的返回值
            DoMoreReturn doMore = (DoMoreReturn)asyncResult.AsyncDelegate;
            int          result = doMore.EndInvoke(asyncResult);

            Thread.Sleep(3000);
            Console.WriteLine("result={0},parameter={1}", result, parameter);
            Console.WriteLine("Callback-End【ThreadId=" + Thread.CurrentThread.ManagedThreadId + "】:" + DateTime.Now);
        }