C# AsyncOperation类---异步操作

来源:互联网 发布:centos 7 系统加固 编辑:程序博客网 时间:2024/05/17 08:49

     

//详细说明

http://technet.microsoft.com/zh-cn/magazine/system.componentmodel.asyncoperation(VS.80).aspx

http://www.cnblogs.com/wangshenhe/archive/2012/05/25/2516842.html

http://blog.csdn.net/zztfj/article/details/6823374

  

//声明

        AsyncOperation asyncOp;

//初始化 -------一般在构造函数里---

            this.asyncOp = AsyncOperationManager.CreateOperation(this);

//使用


                this.asyncOp.Post((o) =>
                {
                        this.BefLabel.Text = stateManager.BefState.StateName.ToString();
                        this.CurrLabel.Text = stateManager.CurrState.StateName.ToString();
                }, null); 

 

 

      /// <summary>非同期操作</summary>
        private AsyncOperation asyncOp;

 

        public Form1()
        {
            InitializeComponent();

            this.asyncOp = AsyncOperationManager.CreateOperation(this);

        }


        // <param name="operation">非同期操作</param>
        public void DispatchToContext(Action operation)
        {
            this.asyncOp.Post((obj) => operation(), null);
        }



           this.DispatchToContext(() =>
          {

                  this.listBox1.Items.AddRange(split);

           });

               //相当于 关闭窗口时报错

                this.Invoke(new Action(() =>
                {
                    this.progressForm.Dispose();
                }));

 

            this.Invoke((MethodInvoker)delegate()
            {
                OnCommandReceive(e.Command);
            });

 

//——————————————————————————————————————————————————————————

        /// <summary>等待执行完再返回结果 及时更新控件</summary>

        public void OnPLCAddressChange(PLCAccessLib.Args.AddressValueChangeEventArgs ev)
        {

            this.asyncOp.SynchronizationContext.Send((msg) =>
                {

                    this.robotForm.OnPLCAddressChange(ev);

                }, null);
        }

 

//相当于

            this.BeginInvoke(new Action(()=>
            {


            }),null);

 


 

 

 

 

 

原创粉丝点击