2013-07-19 87 views
-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 =假前的最后x值,因为重点是去除刚刚从列表中选择的项目。

+0

没有DispatcherTimer线程。此代码在单个线程上运行。 –

+0

指定您认为UI线程应该等待的原因。 –

+0

x的值在timer.start()和selPeople [x] .IsCheck = false之间永不改变;我知道这是因为我的调试器将x的值作为我在顶部设置的默认值-1返回。我需要在TimerTick中捕获x的值,以便在方法结束后将其从列表中删除 – meisenman

回答

0

如果你想调用者的方法来等待这就是所谓的方法的结果,你可以使用异步/等待,在.NET 4.5可

你可以做这样的事情:

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()这是你的计时器相同。

如果您想了解异步/等待我真的建议this

再见我还没有编这个代码的方式。所以它可能有一些语法或其他错误。

+0

我要去尝试只是在一个循环中手动睡觉线程,虽然当我在第一个模拟版本中尝试了这个只有一个人被选中时,我有麻烦,并被告知它是由于thread.sleep – meisenman

+0

如果你使用'Thread 。在用户界面线程中休眠,这会冻结用户界面,你会遇到问题。但是按照我刚才所说的方式,你不会有任何问题。我强烈建议我给你的链接 –

+0

哦,你把TimerTick放在不同的线程中。如果是这种情况,我不需要调用displayImage ... – meisenman

相关问题