zoukankan      html  css  js  c++  java
  • WPF 异步执行

    private void Operate_OnClick(object sender, RoutedEventArgs e)
    {
        AsyncFindBox(); 
        RadWindow.Alert("测试测试!");
    }
    
    private void AsyncFindBox()
    {
        #region 需要将返回结果返回到UI上。 
        //异步任务封装在一个delegate中, 此delegate将运行在后台线程
        Func<string> asyncAction = this.AsyncActionMethod;
        
        //在UI线程中得到异步任务的返回值,并更新UI
        //必须在UI线程中执行
        Action<IAsyncResult> resultHandler = delegate(IAsyncResult asyncResult)
        {
            //异步执行后,将值更新到UI上。
            //string result= asyncAction.EndInvoke(asyncResult);
            //SearchValue.Text=result; 
        };
        //异步任务执行完毕后的callback, 此callback运行在后台线程上.
        //此callback会异步调用resultHandler来处理异步任务的返回值.
        AsyncCallback asyncActionCallback = delegate(IAsyncResult asyncResult)
        {
             Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, resultHandler, asyncResult);
        };
        //在UI线程中开始异步任务,
        //asyncAction(后台线程), asyncActionCallback(后台线程)和resultHandler(UI线程)
        //将被依次执行
         asyncAction.BeginInvoke(asyncActionCallback, null);
         #endregion
         
        #region 不需要将返回结果返回到UI上的。 
        //Func<string> asyncAction = this.AsyncActionMethod;
        //asyncAction.BeginInvoke(null, null);
        
        //Action asyncAction = this.FindBox; //方法无返回值
        //asyncAction.BeginInvoke(null, null);
         #endregion
    }
    
    private string AsyncActionMethod()
    {
        var commandMessage = "";
        Thread.Sleep(5000);
        Console.WriteLine(1111);
        // 发射亮灯  
        BasketLight.SendCmd(5, BasketLight.CmdType.Ready, 0, ref commandMessage);
        return "";
    }
    
    private void FindBox()
    {
        var commandMessage = "";
        Thread.Sleep(5000);
        Console.WriteLine(1111);
        // 发射亮灯  
        BasketLight.SendCmd(5, BasketLight.CmdType.Ready, 0, ref commandMessage); 
    }
  • 相关阅读:
    如何用机器学习强化市场营销活动。
    大数据统计脚本, 分城市订单统计
    宇宙常量与增长黑客。
    病毒传播效果的衡量公式
    浅谈对增长黑客的理解
    大数据分析, 数据挖掘, 机器学习,找到产品改进的爆点。
    R语言的日期运算
    安装语言包-英文(美国)
    selenium page objects
    logging模块
  • 原文地址:https://www.cnblogs.com/vipsoft/p/4535037.html
Copyright © 2011-2022 走看看