Пример #1
0
        public static void Main()
        {
            MethodDelegate deleg = new MethodDelegate(method);

            #region first way
            IAsyncResult asyncRes = deleg.BeginInvoke(4, null, null);
            IAsyncResult asyncRes2 = deleg.BeginInvoke(5, null, null);

            Console.WriteLine("do work");

            Console.WriteLine("when it need get the results");

            Console.WriteLine(deleg.EndInvoke(asyncRes2));
            Console.WriteLine(deleg.EndInvoke(asyncRes));
            #endregion

            #region second way
            AsyncCallback callback = new AsyncCallback(callbackMethod);
            object objectSuppliedState = new object();

            deleg.BeginInvoke(6, callback, objectSuppliedState);
            deleg.BeginInvoke(7, new AsyncCallback(callbackMethod2), new object[] { deleg, objectSuppliedState });

            eventt1.WaitOne();
            eventt2.WaitOne();
            #endregion
        }
Пример #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            MethodDelegate deleg = new MethodDelegate(method);

            #region first way
            IAsyncResult asyncRes = deleg.BeginInvoke(4, null, null);
            IAsyncResult asyncRes2 = deleg.BeginInvoke(5, null, null);

            listBox1.Items.Add("do work");

            listBox1.Items.Add("when it need get the results");

            listBox1.Items.Add(deleg.EndInvoke(asyncRes2));
            listBox1.Items.Add(deleg.EndInvoke(asyncRes));
            #endregion

            #region second way
            AsyncCallback callback = new AsyncCallback(callbackMethod);
            object objectSuppliedState = new object();

            IAsyncResult qwerqwer = deleg.BeginInvoke(6, callback, objectSuppliedState);
            // qwerqwer.AsyncWaitHandle.WaitOne();
            deleg.BeginInvoke(7, new AsyncCallback(callbackMethod2), new object[] { deleg, objectSuppliedState });

            object objectSuppliedState2 = new object();
            AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(objectSuppliedState2);
            System.Threading.SendOrPostCallback deleg2 = new System.Threading.SendOrPostCallback(methodForGettingResults);
            deleg.BeginInvoke(8, new AsyncCallback(callbackMethod3), new object[] { deleg, asyncOp, deleg2, objectSuppliedState });
            #endregion
        }
Пример #3
0
        /// <summary>
        ///  异步方法完成后执行回调
        /// </summary>
        /// <param name="iar"></param>
        public static void MyAsyncCallback(IAsyncResult iar)
        {
            string         str;
            int            iExecThread;
            MethodDelegate dlgt = (MethodDelegate)iar.AsyncState;

            str = dlgt.EndInvoke(out iExecThread, iar);
            Console.WriteLine("the Delegate call returned string:{0},and the number is:{1}", str, iExecThread.ToString());
        }
Пример #4
0
        /*
         * 使用调用模式是要调用 BeginInvoke , 做某些处理主线程, 并调用 EndInvoke() 。
         * 注意不 EndInvoke() 不返回直到异步调用已完成。
         * 此调用模式是有用当要有调用线程正在执行异步调用, 同时工作。
         * 有同时发生工作可改善许多应用程序的性能。
         * 常见任务以异步运行以此方式是文件或网络操作。
         * */
        /// <summary>
        /// 通过EndInvoke()调用模式异步调用模式
        /// </summary>
        public static void DemoEndInvoke()
        {
            string         str;
            int            iExecTread;
            MethodDelegate dlgt = new MethodDelegate(LongRunningMethod);
            IAsyncResult   iar  = dlgt.BeginInvoke(5000, out iExecTread, null, null);

            str = dlgt.EndInvoke(out iExecTread, iar);
            Console.WriteLine("the Delegate call returned string:{0},and the number is:{1}", str, iExecTread.ToString());
        }
Пример #5
0
        /*
         * 由 BeginInvoke() 返回 IAsyncResult 对象有个 IsCompleted 属性异步调用完成后返回 True 。
         * 然后可调用 EndInvoke() 。 如果您应用程序不断工作对不做要长期函数调用已被此调用模式很有用。
         * MicrosoftWindows 应用程序是这样的示例。
         * 主线程的 Windows 应用程序可以继续以执行异步调用时处理用户输入。
         * 它可定期检查 IsCompleted 到调用是否完成。 它调用 EndInvoke 当 IsCompleted 返回 True 。
         * 直到它知道操作已完成因为 EndInvoke() 阻止直到异步操作为完整, 应用程序不调用它。
         * */
        /// <summary>
        ///  异步调用方法通过轮询调用模式
        /// </summary>
        public static void DemoPolling()
        {
            string         str;
            int            iExecThread;
            MethodDelegate dlgt = new MethodDelegate(LongRunningMethod);
            IAsyncResult   iar  = dlgt.BeginInvoke(3000, out iExecThread, null, null);

            while (iar.IsCompleted == false)
            {
                Thread.Sleep(10);
            }
            str = dlgt.EndInvoke(out iExecThread, iar);
            Console.WriteLine("the Delegate call returned string:{0},and the number is:{1}", str, iExecThread.ToString());
        }
Пример #6
0
 /// <summary>
 /// 扫码登录判断
 /// </summary>
 /// <param name="ar"></param>
 public void TakesLoginOpera(IAsyncResult ar)
 {
     if (ar != null)
     {
         _start = true;
         MethodDelegate methodDelegate = ar.AsyncState as MethodDelegate;
         if (methodDelegate != null)
         {
             Image result = methodDelegate.EndInvoke(ar);
             pbQRCode.Image = result;
             object loginStatus = null;
             while (_start && result != null)  //循环判断手机扫描二维码结果
             {
                 loginStatus = loginService.LoginScanDetection();
                 if (loginStatus is Image) //已扫描二维码但未登录
                 {
                     LoginService.tip = 0;
                     this.BeginInvoke((Action) delegate()
                     {
                         lblPrompt.Text    = "请点击手机登录按钮";
                         pbQRCode.SizeMode = PictureBoxSizeMode.CenterImage;  //用户扫描后用户头像
                         pbQRCode.Image    = loginStatus as Image;
                         lblReturn.Visible = true;
                     });
                 }
                 if (loginStatus is string)  //已扫描二维码并完成登录
                 {
                     LoginService.tip = 0;
                     //登录获取Cookie(参考方法 login)
                     loginService.GetSidUin(loginStatus as string);
                     //打开主界面
                     this.BeginInvoke((Action) delegate()
                     {
                         this.DialogResult = DialogResult.OK;
                         this.Close();
                     });
                     break;
                 }
             }
         }
     }
 }