Ejemplo n.º 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
        }
Ejemplo n.º 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
        }
Ejemplo n.º 3
0
 /*
  * 本节, 中示例提供对 BeginInvoke() 函数, 异步调用完成后系统执行回调委托。
  * 回调调用 EndInvoke() 并处理异步调用的结果。
  * 如果启动异步调用线程不需要处理结果是调用此调用模式很有用。
  * 异步调用完成后系统调用线程以外启动线程上调。
  * 若使用此调用模式, 作为第二到最后 - BeginInvoke() 函数的参数必须传递 AsyncCallback 类型的委托。
  * BeginInvoke() 还有最后参数键入 对象 到您可以将任何对象。 当它调用该对象可用于您回调函数。
  * 为此参数一个重要用途是以传递用于初始化调用该委托。
  * 回调函数然后使用与该委托 EndInvoke() 函数来完成调用。 此调用模式是所示。
  * */
 /// <summary>
 /// 异步方法完成后执行回调
 /// </summary>
 public static void DemoCallback()
 {
     MethodDelegate dlgt = new MethodDelegate(LongRunningMethod);
     int            iExecThread;
     AsyncCallback  asyncCallback = new AsyncCallback(MyAsyncCallback);
     IAsyncResult   iar           = dlgt.BeginInvoke(5000, out iExecThread, asyncCallback, dlgt);
 }
        public static void LogNow(List<string> strError)
        {
            IErrorLogging log = new ErrorLogFactory().GetErrorLogInstance();

            MethodDelegate callGenerateFileAsync = new MethodDelegate(log.LogError);
            IAsyncResult ar = callGenerateFileAsync.BeginInvoke(strError, null, null);
        }
Ejemplo n.º 5
0
        public void Say(string words)
        {
            MethodDelegate dlgt = new MethodDelegate(Say);

            // Initiate the asynchronous call.
            IAsyncResult ar = dlgt.BeginInvoke(words, out int iExecThread, null, null);
        }
Ejemplo n.º 6
0
        public ChartController(IHubConnectionContext <dynamic> clients)
        {
            Clients = clients;
            MethodDelegate metho = new MethodDelegate(this.start);

            metho.BeginInvoke(null, null);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Low now method
        /// </summary>
        /// <param name="errors">error list</param>
        public static void LogNow(List <string> errors)
        {
            var log = new ErrorLogFactory().GetErrorLogInstance();
            var callGenerateFileAsync = new MethodDelegate(log.LogError);

            callGenerateFileAsync.BeginInvoke(errors, null, null);
        }
Ejemplo n.º 8
0
        /**
         * Metodo delegato per inizio chiamata asincrona volta a processare
         * la finestra.
         */
        public void ManageWindow(List <Packet> buffer, bool isLast)
        {
            List <Packet>  tmp = new List <Packet>(buffer);
            MethodDelegate delegateWindowWork = new MethodDelegate(this.SetWindowAndWork);

            delegateWindowWork.BeginInvoke(tmp, isLast, null, null);
        }
Ejemplo n.º 9
0
        public static void LogNow(List <string> strError)
        {
            IErrorLogging log = new ErrorLogFactory().GetErrorLogInstance();

            MethodDelegate callGenerateFileAsync = new MethodDelegate(log.LogError);
            IAsyncResult   ar = callGenerateFileAsync.BeginInvoke(strError, null, null);
        }
Ejemplo n.º 10
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());
        }
Ejemplo n.º 11
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());
        }
Ejemplo n.º 12
0
        public void Update()
        {
            while (serialPort.BytesToRead >= 285)
            {
                ReadData();
            }
            if (serialPort.BytesToRead == 1)
            {
                serialPort.ReadByte();
            }

            serialPort.Write(new byte[] { 0x38 }, 0, 1);
            alternativeRequest.BeginInvoke(null, null);
        }
Ejemplo n.º 13
0
        public override void Update()
        {
            while (FTD2XX.BytesToRead(handle) >= 285)
            {
                ReadData();
            }
            if (FTD2XX.BytesToRead(handle) == 1)
            {
                FTD2XX.ReadByte(handle);
            }

            FTD2XX.Write(handle, new byte[] { 0x38 });
            alternativeRequest.BeginInvoke(null, null);
        }
Ejemplo n.º 14
0
        public void Login()
        {
            pbQRCode.Image    = null;
            pbQRCode.SizeMode = PictureBoxSizeMode.Zoom;
            lblPrompt.Text    = "手机微信扫一扫登录";
            MethodDelegate methodDelegate = loginService.GetQRCode;

            methodDelegate.BeginInvoke(TakesLoginOpera, methodDelegate);
            #region test
            //   _task = new Task(() =>
            //   {
            //       Image qrCode = GetQRCode();
            //       if (qrCode != null)
            //       {
            //           this.BeginInvoke((Action)delegate ()
            //           {
            //               pbQRCode.Image = qrCode;
            //           });
            //           object loginStatus = null;
            //           while (true)  //循环判断手机扫描二维码结果
            //           {
            //               loginStatus = LoginScanDetection();
            //               if (loginStatus is Image) //已扫描二维码但未登录
            //               {
            //                   this.BeginInvoke((Action)delegate ()
            //                   {
            //                       lblPrompt.Text = "请点击手机登录按钮";
            //                       pbQRCode.SizeMode = PictureBoxSizeMode.CenterImage;  //用户扫描后用户头像
            //                       pbQRCode.Image = loginStatus as Image;
            //                       lblReturn.Visible = true;
            //                   });
            //               }
            //               if (loginStatus is string)  //已扫描二维码并完成登录
            //               {
            //                   //登录获取Cookie(参考方法 login)
            //                   GetSidUin(loginStatus as string);
            //                   //打开主界面
            //                   this.BeginInvoke((Action)delegate ()
            //                   {
            //                       this.DialogResult = DialogResult.OK;
            //                       this.Close();
            //                   });
            //                   break;
            //               }
            //           }
            //       }
            //   });
            //   _task.Start();
            #endregion
        }
Ejemplo n.º 15
0
        public sealed override void Update()
        {
            while (Ftd2xx.BytesToRead(_handle) >= 285)
            {
                ReadData();
            }

            if (Ftd2xx.BytesToRead(_handle) == 1)
            {
                Ftd2xx.ReadByte(_handle);
            }

            Ftd2xx.Write(_handle, new byte[] { 0x38 });
            _alternativeRequest.BeginInvoke(null, null);
        }
Ejemplo n.º 16
0
        public void Update()
        {
            try {
                while (serialPort.IsOpen && serialPort.BytesToRead >= 285)
                {
                    ReadData();
                }
                if (serialPort.BytesToRead == 1)
                {
                    serialPort.ReadByte();
                }

                serialPort.Write(new byte[] { 0x38 }, 0, 1);
                alternativeRequest.BeginInvoke(null, null);
            } catch (InvalidOperationException) {
                foreach (Sensor sensor in active)
                {
                    sensor.Value = null;
                }
            }
        }
Ejemplo n.º 17
0
        private async void btnRunAsync_Click(object sender, EventArgs e)
        {
            MethodDelegate scr = null;

            try
            {
                scr = await Task.Run(() => CSScript.CodeDomEvaluator.CreateDelegate(scintilla.Text));
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Exception generating script.");
                return;
            }
            try
            {
                scr.BeginInvoke(new[] { logger }, null, null);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Exception running script.");
                return;
            }
        }
Ejemplo n.º 18
0
        protected override void OnStart(string[] args)
        {
            MethodDelegate metho = new MethodDelegate(this.start);

            metho.BeginInvoke(null, null);
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Low now method
 /// </summary>
 /// <param name="errors">error list</param>
 public static void LogNow(List<string> errors)
 {
     var log = new ErrorLogFactory().GetErrorLogInstance();
     var callGenerateFileAsync = new MethodDelegate(log.LogError);
     callGenerateFileAsync.BeginInvoke(errors, null, null);
 }
Ejemplo n.º 20
0
        private void button1_Click(object sender, EventArgs e)
        {
            List<Guid> list = new List<Guid>();

            string ex = "";
            DataTable tb = MQMsgCommonHelper.QueryMQLog("1=1",out ex);
            if (tb != null)
            {
                foreach (DataRow row in tb.Rows)
                {
                    //list.Add(new Guid(row["GUID"].ToString()));
                    MethodDelegate test = new MethodDelegate(OnMultiTest);
                    test.BeginInvoke(new Guid(row["GUID"].ToString()), null, null);
                }
            }
            

        }
Ejemplo n.º 21
0
 protected override void OnStart(string[] args)
 {
     MethodDelegate metho = new MethodDelegate(this.start);
     metho.BeginInvoke(null, null);
 }
Ejemplo n.º 22
0
 public static void Call(string message)
 {
     var          dlgt = new MethodDelegate(SaveData);
     IAsyncResult ar   = dlgt.BeginInvoke(message, null, null);
 }
Ejemplo n.º 23
0
 public ChartController(IHubConnectionContext<dynamic> clients)
 {
     Clients = clients;
     MethodDelegate metho = new MethodDelegate(this.start);
     metho.BeginInvoke(null, null);
 }