Built in background-scheduling system in .NET?
Posted
by Lasse V. Karlsen
on Stack Overflow
See other posts from Stack Overflow
or by Lasse V. Karlsen
Published on 2010-06-02T13:25:16Z
Indexed on
2010/06/05
21:42 UTC
Read the original article
Hit count: 154
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.
© Stack Overflow or respective owner