Ondrej Janacek
Ondrej Janacek

Reputation: 12616

Choosing right timer for scheduler

I'm making my own scheduler, which is going to be used in one of my WPF application.

Here is code.

// Interface for a scheduled task.
public interface IScheduledTask
{
    // Name of a task.
    string Name { get; }

    // Indicates whether should be task executed or not.
    bool ShouldBeExecuted { get; }

    // Executes task.
    void Execute();
    }

// Template for a scheduled task.
public abstract class PeriodicScheduledTask : IScheduledTask
{
    // Name of a task.
    public string Name { get; private set; }

    // Next task's execute-time.
    private DateTime NextRunDate { get; set; }

    // How often execute?
    private TimeSpan Interval { get; set; }

    // Indicates whether task should be executed or not. Read-only property.
    public bool ShouldBeExecuted 
    { 
        get 
        {
            return NextRunDate < DateTime.Now;
        }
    }

    public PeriodicScheduledTask(int periodInterval, string name)
    {
        Interval = TimeSpan.FromSeconds(periodInterval);
        NextRunDate = DateTime.Now + Interval;
        Name = name;
    }

    // Executes task.
    public void Execute()
    {
        NextRunDate = NextRunDate.AddMilliseconds(Interval.TotalMilliseconds);
        Task.Factory.StartNew(new Action(() => ExecuteInternal()));
    }

    // What should task do?
    protected abstract void ExecuteInternal();
}

// Schedules and executes tasks.
public class Scheduler
{
    // List of all scheduled tasks.
    private List<IScheduledTask> Tasks { get; set; }

    ... some Scheduler logic ...
}

Now, I need to choose right .net timer for scheduler. There should be subscribed event tick/elapsed inside, which goes through tasklist and checks whether some task should be executed and then execute it by calling task.Execute().

Some more information. I need interval of timer set on 1 sec because some tasks I am creating needs to be executed every second, two, or more.

Do I need run timer on new thread to enable user's actions on form? Which timer is most suitable for this Scheduler?

Upvotes: 0

Views: 1292

Answers (2)

A. Wilson
A. Wilson

Reputation: 688

I would use System.Timers.Timer. From the MSDN documentation:

The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.

I don't think you should have to manually start it on a separate thread. I've never had it steal CPU time from the UI, although my development has been mostly in Winforms, not WPF.

Upvotes: 1

Slugart
Slugart

Reputation: 4680

You should use the DispatcherTimer as it is integrated into the dispatcher queue on the same thread that it is created (in your case the UI thread):

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();

Upvotes: 0

Related Questions