我正在使用 wpf DispatcherTimer,我想将它使用到 for 循环中,我如何使用它..
我的代码在这里..
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += (s, e) =>
{
for (i = 0; i < 10; i++)
{
obsValue.Add(new Entities(i));
timer.Interval = TimeSpan.FromSeconds(30);
timer.Start();
}
};
谢谢......
最佳答案
当您启动计时器并将 Interval
设置为 30 秒
时,其 Tick
事件将每 30 秒引发一次。
现在,我从你的问题中了解到,你想每 30 秒添加一条记录。
这是您可以执行的操作。请注意,它不需要 for 循环
,但您仍然需要维护当前索引。为此,您可以使用私有(private)字段
或带有 lambda 的局部变量
。
示例:
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(30);
Int32 index = 0, maxValue = 10;
timer.Tick += (s, e) =>
{
obsValue.Add(new Entities(index));
index ++; // increment index
// Stop if this event has been raised max number of times
if(index > maxValue) timer.Stop();
};
timer.Start();
关于wpf - 如何将 DispatcherTimer 设置到 for 循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4544147/