beginInvoke用法

74 阅读1分钟

在background worker调用UI的时候使用,异步操作

无参数用法

this.BeginInvoke(new Action(() =>
            {
                SetButtonStatus(false, false);
                this.mWaitForm = new WaitForm("Please wait", "Query supported Flash size...", this.backgroundWorker_QuerySpicMode);
                this.mWaitForm.ShowDialog(this);
                //中间就是要执行的函数
            }));

有参数用法

BeginInvoke(new UpdatePincontrolRightDelegate(UpdatePinControlRight), new object[] { pinControl, this, this.mPinmuxInterface });

private delegate void UpdatePincontrolRightDelegate(RtkPinControlRight pinControl, RtkPackageInterface packageInterface, RtkPinmuxInterface pinmuxInterface);

private void UpdatePinControlRight(RtkPinControlRight pinControl, RtkPackageInterface packageInterface, RtkPinmuxInterface pinmuxInterface)
        {
            pinControl.Initialize(this, this.mPinmuxInterface);
        }

可以使用this.InvokeRequired来询问是否需要异步

if (this.InvokeRequired)
            {
                this.BeginInvoke(
                    new OnTestResultUpdateDelegate(HandleTestResultUpdate),
                    new object[] { result });
            }
            else
            {
                HandleTestResultUpdate(result);
            }