0

我需要能够影响 TimerCallback 中的多个 Textblock 元素。目前,我有这个:

t = new Timer(tc, myLabel, 0, 1000);

但是,在 tc 中,我想做以下事情:

myLabel.Text = "ABC";
myLabel2.Text = "DEF";

我还没有弄清楚如何将多个对象传递给我的 TimerCallback。每当我尝试在 TimerCallback 中设置 TextBlock 时,都会出现错误:

System.UnauthorizedAccessException

我尝试在对象数组中编码,但没有奏效。

4

1 回答 1

2

你需要使用DispatchTimer. 你必须这样做,因为它是一个单独的线程。

     private void InitializeTimers()
            {
                DispatcherTimer tmr = new DispatcherTimer();
                tmr.Interval = TimeSpan.FromSeconds(1);
                tmr.Tick += OnTimerTick;
                tmr.Start();
            }

  private void OnTimerTick(object sender, EventArgs args)
        {
              //do whatever you want
        }
于 2012-04-09T23:49:50.827 回答