-1

我正在使用一个 DispatcherTimer,它调用一个方法在不同的线程中执行。我正在运行一个不需要真正担心效率低下的简单程序。如何让 UI 线程等待 DispatcherTimer 完成,同时仍然允许 DispatcherTimer 线程对 UI 进行更改?

*我知道这可能是半重复的,但是我看到的示例是针对特定情况的。谢谢

private void spinButton_Click(object sender, RoutedEventArgs e)
    {

        minSelTextBlock.Text = "";

            Index = 0;
            maxIndex = rnd.Next(40, 60);
            DispatcherTimer timer;
            timer = new DispatcherTimer(DispatcherPriority.Normal);
            timer.Interval = TimeSpan.FromMilliseconds(60);
            timer.Tick += new EventHandler(TimerTick);
            timer.Start();
            selPeople[x].IsCheck = false;
            displayCount++;

            Index = 0;
            maxIndex = rnd.Next(40, 60);
            timer = new DispatcherTimer(DispatcherPriority.Normal);
            timer.Interval = TimeSpan.FromMilliseconds(60);
            timer.Tick += new EventHandler(TimerTick);
            timer.Start();
            selPeople[x].IsCheck = false;
            displayCount++;

            displayImage2b.Source = new BitmapImage(new Uri(selPeople[0].ImgPath));          
    }

 private void TimerTick(object sender, EventArgs e)
    {
        minSelTextBlock.Text = "";

        x = rnd.Next(0, selPeople.Count);
        while (x == temp)
        {
            x = rnd.Next(0, selPeople.Count);
        }

        if (displayCount == 0)
            displayImage1a.Source = new BitmapImage(new Uri(selPeople[x].ImgPath));
        if (displayCount == 1)
            displayImage2a.Source = new BitmapImage(new Uri(selPeople[x].ImgPath));


        if (++Index >= maxIndex)
        {
            ((DispatcherTimer)sender).Stop();
        }
        Index++;
        temp = x;
    }

基本上我需要 TimerTick 在 selPeople[x].IsCheck = false 之前获得最终的 x 值,因为重点是删除刚刚从列表中选择的项目。

4

1 回答 1

0

如果您希望调用者方法等待被调用方法的结果,您可以使用 .Net 4.5 中可用的 Async/Await

你可以这样做:

private async void spin()
    {

        minSelTextBlock.Text = "";

            Index = 0;
            maxIndex = rnd.Next(40, 60);
            DispatcherTimer timer;
            timer = new DispatcherTimer(DispatcherPriority.Normal);
            timer.Interval = TimeSpan.FromMilliseconds(60);
            timer.Tick += new EventHandler(StartTicker);
            timer.Start();
            selPeople[x].IsCheck = false;
            displayCount++;

            Index = 0;
            maxIndex = rnd.Next(40, 60);

            await Ticker();

            selPeople[x].IsCheck = false;
            displayCount++;

            displayImage2b.Source = new BitmapImage(new Uri(selPeople[0].ImgPath));          
    }


  Private Task<void> StartTicker()
  {
    Task.Run<void>(()=>Ticker());
  }

private void Ticker()
    {
      while(your condition is true)
      {
        minSelTextBlock.Text = "";

        x = rnd.Next(0, selPeople.Count);
        while (x == temp)
        {
            x = rnd.Next(0, selPeople.Count);
        }

        if (displayCount == 0)
            displayImage1a.Source = new BitmapImage(new Uri(selPeople[x].ImgPath));
        if (displayCount == 1)
            displayImage2a.Source = new BitmapImage(new Uri(selPeople[x].ImgPath));


        if (++Index >= maxIndex)
        {
            break;
        }
        Index++;
        temp = x;
         Thread.Sleep(60); 
      }
    }

而不是计时器,我使用了一个While LoopThread.Sleep()你的计时器相同的。

如果你想了解 async/await 我真的推荐这个

再见,我还没有编译这段代码。所以它可能有一些语法或其他错误。

于 2013-07-19T18:57:30.013 回答