gpt4 book ai didi

wpf - 我如何将DispatcherTimer设置为for循环

转载 作者:行者123 更新时间:2023-12-03 12:53:05 24 4
gpt4 key购买 nike

我正在使用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 seconds来启动计时器时,其Tick事件将每30秒引发一次。
现在,我从您的问题中了解到,您想每30秒添加一条记录。
这是您可以做的。请注意,它不需要for loop,但您仍然需要维护当前索引。为此,您可以使用private fieldlocal variable with 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com