如何在 WPF 中使用动态调度程序计时器对象?



我想在我的wpf项目中使用动态DispatcherTimer object。我有一个像下面的计时器;

DispatcherTimer timer_log = new DispatcherTimer();

我有一个列表,名称是log_durations。该列表具有持续时间。我使用此方法管理计时器对象。

int Log = 0,LogDuration=0;
public void set_timer_log(int interval)
{
timer_log.Interval = TimeSpan.FromMilliseconds(interval);
timer_log.Tick += timer_log_Tick;
timer_log.IsEnabled = true;
}
private void timer_log_Tick(object sender, EventArgs e)
{
timer_log.IsEnabled = false;
try
{
if (Log < logs.Length)
{
add_package_log(logs[Log]);
Log++;
LogDuration++;
set_timer_log(log_durations[LogDuration]);
}
}
catch (Exception ex)
{
MessageBox.Show("Timer Log Tick  Function Error:" + ex.Message);
}
}

因此,当我对列表中的日志持续时间求和时,例如 10 分钟。但日志在 30 秒内显示。 简而言之,计时器对象无法正常工作。问题出在哪里?我看不见。

这段代码工作得很清楚。

DispatcherTimer timer_log = new DispatcherTimer();
timer_log.Tick += timer_log_Tick;
int Log = 0,LogDuration=0;
public void set_timer_log(int interval)
{
timer_log.Interval = TimeSpan.FromMilliseconds(interval);
timer_log.IsEnabled = true;
}
private void timer_log_Tick(object sender, EventArgs e)
{
timer_log.IsEnabled = false;
try
{
if (Log < logs.Length)
{
add_package_log(logs[Log]);
Log++;
LogDuration++;
set_timer_log(log_durations[LogDuration]);
}
}
catch (Exception ex)
{
MessageBox.Show("Timer Log Tick  Function Error:" + ex.Message);
}
}

最新更新