コード例 #1
0
        private static void DelegateCallback(IAsyncResult iresult)
        {
            Console.WriteLine($"Getting callback on Thread {Thread.CurrentThread.ManagedThreadId}");
            AsyncResult asyncResult = (AsyncResult)iresult;
            AsyncInvoke method1     = (AsyncInvoke)asyncResult.AsyncDelegate;

            int retVal = method1.EndInvoke(asyncResult);//在回调委托里面EndInvoke

            Console.WriteLine($"retVal (CallBack):{retVal}");
        }
コード例 #2
0
        public void PollAsyncDelegate()
        {
            AsyncInvoke  MI = new AsyncInvoke(TestAsyncInvoke.Method1);
            IAsyncResult AR = MI.BeginInvoke(null, null);

            while (!AR.IsCompleted)
            {
                System.Threading.Thread.Sleep(100);
                Console.WriteLine('.');
            }
            Console.WriteLine("Finished Polling");

            try
            {
                int retVal = MI.EndInvoke(AR);
                Console.WriteLine("RetVal (Polling): " + retVal);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }