C# AsyncOperation类---异步操作

     

//详细说明

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);

 


 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值