Built in background-scheduling system in .NET?

ぐ巨炮叔叔 提交于 2019-12-01 22:34:37

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.

Use quartz.net (open source).

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.

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.

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.

In addition to the answers supplied, this can also be done with the threadpool:

public static RegisteredWaitHandle Schedule(Action action, TimeSpan dueTime)
{
    var handle = new ManualResetEvent(false);
    return ThreadPool.RegisterWaitForSingleObject(
                                         handle, 
                                         (s, t) =>
                                         {
                                             action();
                                             handle.Dispose();
                                         }, 
                                         null, 
                                         (int) dueTime.TotalMilliseconds, true);
}

The RegisteredWaitHandle has an Unregister method that can be used to cancel the request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!