WPF 异步显示

纠结老半天哦  WPF 和WinForm 的异步不一样的

WinFrom:

定义一个委托 

 delegate void ShowRunTimeDelegate(string time);//显示运行时间

  

        #region  RunTime 异步Timer事件方法
        private void RunTime(object obj)
        {
            RunTimeSeconds++;

            long td = 0, th = 0, tm = 0, ts = 0;

            td = RunTimeSeconds / (60 * 60 * 24);
            th = (RunTimeSeconds % (60 * 60 * 24)) / (60 * 60);
            tm = ((RunTimeSeconds % (60 * 60 * 24)) % (60 * 60)) / 60;
            ts = ((RunTimeSeconds % (60 * 60 * 24)) % (60 * 60)) % 60;
            ShowRunTime("系统已运行:" + td + "天" + th + "时" + tm + "分" + ts + "秒");
        }
        #endregion

        #region 显示系统运行时间   重点就是ShowRunTime这个方法
        /// <summary>
        /// 显示系统运行时间
        /// </summary>
        /// <param name="time"></param>
        private void ShowRunTime(string time)
        {
            if (!InvokeRequired)
            {
                lb_RunTime.Text = time;
            }
            else
            {
                ShowRunTimeDelegate srt = new ShowRunTimeDelegate(ShowRunTime);
                base.Invoke(srt, new object[] { time });
            }
        }
        #endregion

  

WPF:

定义一个委托:

delegate void ShowRunTimeDelegate(string time);//显示运行时间

  

//此方法为线程
private void ThreadTest()
        {
            object[] param = {  aaa++ };
      
            while (true)
            {
                tb_test.Dispatcher.BeginInvoke(new TestDelegate(testa), new object[] { aaa++ });  //异步显示
              
                Thread.Sleep(1);
            }
        }
        private void testa(int a)
        {
            lb_RunTime.Text = a.ToString();
        }

  

 

 

posted @ 2013-09-17 19:37  未来由我开启  阅读(477)  评论(0编辑  收藏  举报