Beispiel #1
0
        static void funccallback(IAsyncResult ar)
        {
            TakesAwhileDelegate d = ar.AsyncState as TakesAwhileDelegate;
            int result            = d.EndInvoke(ar);

            Console.WriteLine("result: {0}", result);
        }
Beispiel #2
0
        static void TestCallback()
        {
            TakesAwhileDelegate d  = TakesAWhile;
            IAsyncResult        ar = d.BeginInvoke(1, 3000, funccallback, d);

            //若在委托结束之前不等待委托完成其任务就结束主线程,委托线程就会停止。
            for (int i = 0; i < 20; i++)
            {
                Console.Write(".");
                Thread.Sleep(100);
            }
        }
Beispiel #3
0
        static void TestIsCompleted()
        {
            TakesAwhileDelegate d  = TakesAWhile;
            IAsyncResult        ar = d.BeginInvoke(1, 3000, null, null);

            while (!ar.IsCompleted)//若在委托结束之前不等待委托完成其任务就结束主线程,委托线程就会停止。
            {
                Console.Write(".");
                Thread.Sleep(100);
            }
            int result = d.EndInvoke(ar);

            Console.WriteLine("result: {0}", result);
        }
Beispiel #4
0
        static void TakesAwhileCompleted(IAsyncResult ar)
        {
            if (ar == null)
            {
                throw new ArgumentNullException("ar");
            }

            TakesAwhileDelegate dl = ar.AsyncState as TakesAwhileDelegate;

            Trace.Assert(dl != null, "Invoke object type");

            int result = dl.EndInvoke(ar);

            Console.WriteLine("result: {0}", result);
        }
Beispiel #5
0
        public void Test1()
        {
            Console.WriteLine(string.Format("Step1: {0}, Test1 thread ID = {1}",
                                            DateTime.Now,
                                            Thread.CurrentThread.ManagedThreadId));

            TakesAwhileDelegate d1 = TakesAWhile;

            var ar = d1.BeginInvoke(1, 3000, null, null);

            ar.AsyncWaitHandle.WaitOne();

            var result = d1.EndInvoke(ar);

            Console.WriteLine(string.Format("Step5: {0}, Result = {1}", DateTime.Now, result));
        }
Beispiel #6
0
        static void TestWaitOne()
        {
            TakesAwhileDelegate d  = TakesAWhile;
            IAsyncResult        ar = d.BeginInvoke(1, 3000, null, null);

            //若在委托结束之前不等待委托完成其任务就结束主线程,委托线程就会停止。
            while (true)
            {
                Console.Write(".");
                if (ar.AsyncWaitHandle.WaitOne(100, false))
                {
                    Console.WriteLine("Can get the result now");
                    break;
                }
                Thread.Sleep(100);
            }
            int result = d.EndInvoke(ar);

            Console.WriteLine("result: {0}", result);
        }
Beispiel #7
0
        public Form1()
        {
            InitializeComponent();

            //  异步委托2 Lambda [1/29/2019 yongchao]
            TakesAwhileDelegate dl = TakesAwhile;

            dl.BeginInvoke(1, 5000,
                           ar =>
            {
                int result = dl.EndInvoke(ar);
                Console.WriteLine("result: {0}", result);
            },
                           null);
            for (int i = 0; i < 100; i++)
            {
                Console.Write("*");
                Thread.Sleep(200);
            }
            ;
        }