views:

91

answers:

5

I ask though I doubt there is any such system.

Basically I need to schedule tasks to execute at some point in the future (usually no more than a few seconds or possibly minutes from now), and have some way of cancelling that request unless too late.

Ie. code that would look like this:

var x = Scheduler.Schedule(() => SomethingSomething(), TimeSpan.FromSeconds(5));
...
x.Dispose(); // cancels the request

Is there any such system in .NET? Is there anything in TPL that can help me?

I need to run such future-actions from various instances in a system here, and would rather avoid each such class instance to have its own thread and deal with this.

Also note that I don't want this (or similar, for instance through Tasks):

new Thread(new ThreadStart(() =>
{
    Thread.Sleep(5000);
    SomethingSomething();
})).Start();

There will potentially be a few such tasks to execute, they don't need to be executed in any particular order, except for close to their deadline, and it isn't vital that they have anything like a realtime performance concept. I just want to avoid spinning up a separate thread for each such action.

+3  A: 

Use quartz.net (open source).

Tom Cabanski
I will certainly look into that, but unfortunately this is for a class library and I can't have any external references. If there isn't anything built in, I'll have to make something myself. Thankfully my needs are small in this regards.
Lasse V. Karlsen
+4  A: 

Since you don't want to have external reference, you might want to have a look at System.Threading.Timer, or System.Timers.Timer. Note that those classes are technically very different from the WinForms Timer component.

Lucero
Will starting multiple such timers spawn multiple threads? Or do they use some common scheduler thread that handles everything?
Lasse V. Karlsen
They use thread pool threads for callback execution and don't have any additional management threads AFAIR. At least they don't have dedicated threads for sure; the actual sheduling is implemented by the runtime and should be pretty efficient, especially for `System.Threading.Timer`.
Lucero
Thanks, this was exactly what I was looking for.
Lasse V. Karlsen
You're welcome. :-)
Lucero
+1  A: 

While not explicitly built-in to .NET, have you considered writing EXEs that you can schedule via Windows Scheduled Tasks? If you're using .NET, you'll probably be using Windows, and isn't this exactly what Scheduled Tasks is for?

I'm not aware of how to integrate scheduled tasks into a .NET solution, but surely you could write components that get called from scheduled tasks.

Ben McCormack
This is not an option. There will be many lightweight tasks to execute here.
Lasse V. Karlsen
+1  A: 

Well.....Actually you can do exactly what you asked....

  IDisposable kill = null;

  kill = Scheduler.TaskPool.Schedule(() => DoSomething(), dueTime);

  kill.Dispose();

Using Rx the Reactive Framework from our buddies at Microsoft :)

Rx is quite amazing. I've all but replaced events with it. It is just plain yummy...

Hi I'm Rusty I'm an Rx addict...and I like it.

Rusty
+1  A: 

If you are willing ot use .NET 4, then the new System.Threading.Tasks provides a way to create custom schedulers for tasks. I haven't looked into it too closely, but it seems you could derive your own scheduler and let it run tasks as you see fit.

Mystere Man