我正在使用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值,因为重点是去除刚刚从列表中选择的项目。
没有DispatcherTimer线程。此代码在单个线程上运行。 –
指定您认为UI线程应该等待的原因。 –
x的值在timer.start()和selPeople [x] .IsCheck = false之间永不改变;我知道这是因为我的调试器将x的值作为我在顶部设置的默认值-1返回。我需要在TimerTick中捕获x的值,以便在方法结束后将其从列表中删除 – meisenman