Muammer
Muammer

Reputation: 404

How to use dynamic DispatcherTimer object in WPF?

I want to use dynamic DispatcherTimer object in my wpf project. I have a timer like below;

DispatcherTimer timer_log = new DispatcherTimer();

I have a list that name is log_durations. The list has durations. And I use this methods to manage timer object.

 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);
        }
    }

So when I sum log durations in the list, for example 10 minute. But logs are showing in 30 second. In brief, timer object not works correctly. What is the problem? I couldn't see.

Upvotes: 1

Views: 249

Answers (1)

Muammer
Muammer

Reputation: 404

This code works clearly.

 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);
    }
}

Upvotes: 2

Related Questions