自动处理跨线程的相关操作-及异步调用方式

------------------------------------------------------------------------------------
--------------------自动处理跨线程的相关操作-及异步调用方式----------------------------
        #region 异步执行方法

        /// <summary>
        /// 异步调用方式
        /// </summary>
        void AsyncMethod(object argObj = null)
        {
            _delegateMethod1 = ExecByAsync;
            _delegateMethod1.BeginInvoke(argObj, DoneCallBack, _delegateMethod1);
        }
        DelegateMethod _delegateMethod1;//实例化委托
        delegate bool DelegateMethod(object argObj);//声明委托

        /// <summary>
        /// 回调函数
        /// </summary>
        void DoneCallBack(IAsyncResult asyncResult)
        {
            _delegateMethod1.EndInvoke(asyncResult);
        }

        /// <summary>
        /// 异步执行的方法
        /// </summary>
        bool ExecByAsync(object argObj)
        {
      //do something..
      return true;
        }

        #endregion

        /// <summary>
        /// 更新进度(自动处理跨线程的相关操作)
        /// </summary>
        private void Setlbl(object argMsg)
        {
            if (lblDealWithProgress.InvokeRequired)
            {
                var callBack = new InvokeCallBack1(Setlbl);
                lblDealWithProgress.Invoke(callBack, new[] { argMsg });
            }
            else
                lblDealWithProgress.Text = argMsg.ToString();
        }

        private delegate void InvokeCallBack1(object argMsg);
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

你可能感兴趣的:(异步调用)