Click here to Skip to main content
Click here to Skip to main content

High Precision Task Scheduler

, 25 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
High precision tool for scheduling finely grained tasks (w/in a single process)

Introduction

This project provides a simple, scalable mechanism for scheduling arbitrary events with a high degree of precision. Key features of this approach include: high-precision timing to the extent provided by the architecture, good scalability that does not require the use of one thread per pending event, flexible choice of actions that can be scheduled. This project assumes the use of a single process, cross-process scheduling is outside this scope.

Background

In certain high-precision environments, such as high-frequency trading, it is important to schedule tasks with a great degree of precision. For instance, you may want to cancel an order exactly 1.5 seconds after it has been submitted. One solution is to spin off separate threads for each task and using Thread.Sleep() to cause the thread to wake up at the given time. While threads are cheap, this approach requires one thread per pending task and may not scale well.

Alternatively, the technique used here relies on a tight loop that wakes up every 20 milliseconds and checks for any pending events that have been scheduled for the current period. An arbitrary number of events can be scheduled for any 20 millisecond period.

Using the Code

The TaskScheduler has the main event loop, which is invoked when the singleton instance is constructed. To schedule an event, simply provide the ScheduledEventDelegate (i.e. method to execute), together with a TimeSpan indicating how long to delay the start of the event as well as the list of input parameters. E.g.

TaskScheduler.Instance.AddToDoEvent
	(TaskScheduler.Instance.Action1, TimeSpan.FromMilliseconds(5000), "anObject");

To shut down the event loop, simply set the Program.m_KeepRunning property to false.

History

  • 24th March, 2009: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ronnotel
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMichael E. Jones26-Mar-09 5:39 
General[My vote of 2] hmm... more thoughts Pinmemberjohannesnestler26-Mar-09 3:25 
I think your code doesn't do what it promises. I don't see how you can provide "high" precision timing through that code. It's for sure a nice and quick implementation of a scheduler, but with Thread.Sleep i think you don't go arround context switches etc. and as PIEBALDconsult mentioned you are running the task in the same thread as your "timer-loop", so the loop code is not executed during long running tasks. I'd suggest that you execute the scheduled tasks in at least one different thread, and maybe you should use something that is related to realtime to control the loop and the task duration. (System.Diagnostics.StopWatch?). I'm not aware of any solution for executing multiple tasks that have longer running times than the scheduled intervalls, except using a thread(-pool) for each task... OMG | :OMG:
Please correct me if I missed some important point in your solution, but you are not discussing your source in the article, and I only did a quick look...
GeneralThoughts PinmemberPIEBALDconsult25-Mar-09 11:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 25 Mar 2009
Article Copyright 2009 by ronnotel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid