.NET 内置后台调度系统?
尽管我怀疑是否有这样的系统,但我还是问了。
基本上,我需要安排任务在将来的某个时刻执行(通常不超过几秒钟或可能是几分钟),并且有某种方法取消该请求,除非为时已晚。
IE。代码看起来像这样:
var x = Scheduler.Schedule(() => SomethingSomething(), TimeSpan.FromSeconds(5));
...
x.Dispose(); // cancels the request
.NET 中是否有这样的系统? TPL 有什么可以帮助我的吗?
我需要从系统中的各个实例运行此类未来操作,并且宁愿避免每个此类实例拥有自己的线程并处理此问题。
另请注意,我不希望这样(或类似的,例如通过任务):
new Thread(new ThreadStart(() =>
{
Thread.Sleep(5000);
SomethingSomething();
})).Start();
可能会执行一些此类任务,它们不需要以任何特定顺序执行,除了接近截止日期之外,他们是否拥有诸如实时性能概念之类的东西并不重要。我只是想避免为每个此类操作创建一个单独的线程。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于您不想有外部引用,因此您可能需要查看
System.Threading.Timer
,或System.Timers.Timer
。请注意,这些类在技术上与 WinForms Timer 组件有很大不同。Since you don't want to have external reference, you might want to have a look at
System.Threading.Timer
, orSystem.Timers.Timer
. Note that those classes are technically very different from the WinForms Timer component.使用quartz.net(开源)。
Use quartz.net (open source).
嗯......实际上你可以完全按照你的要求去做......
使用Rx 响应式框架,来自我们 Microsoft 的朋友:)
Rx 非常惊人。我几乎用它取代了事件。它只是简单美味......
嗨,我是 Rusty,我是一个 Rx 瘾君子......我喜欢它。
Well.....Actually you can do exactly what you asked....
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.
虽然没有明确内置到 .NET,但您是否考虑过编写可以通过 Windows 计划任务进行计划的 EXE?如果您使用 .NET,您可能也会使用 Windows,这不正是计划任务的用途吗?
我不知道如何将计划任务集成到 .NET 解决方案中,但您肯定可以编写从计划任务中调用的组件。
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.
如果您愿意使用 .NET 4,那么新的 System.Threading.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.
除了提供的答案之外,还可以使用线程池来完成此操作:
RegisteredWaitHandle
有一个 Unregister 方法,可用于取消请求In addition to the answers supplied, this can also be done with the threadpool:
The
RegisteredWaitHandle
has an Unregister method that can be used to cancel the request