.NET 内置后台调度系统?

发布于 2024-09-04 02:35:24 字数 620 浏览 6 评论 0原文

尽管我怀疑是否有这样的系统,但我还是问了。

基本上,我需要安排任务在将来的某个时刻执行(通常不超过几秒钟或可能是几分钟),并且有某种方法取消该请求,除非为时已晚。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

若有似无的小暗淡 2024-09-11 02:35:24

由于您不想有外部引用,因此您可能需要查看 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, or System.Timers.Timer. Note that those classes are technically very different from the WinForms Timer component.

帅的被狗咬 2024-09-11 02:35:24

使用quartz.net(开源)。

Use quartz.net (open source).

情感失落者 2024-09-11 02:35:24

嗯......实际上你可以完全按照你的要求去做......

  IDisposable kill = null;

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

  kill.Dispose();

使用Rx 响应式框架,来自我们 Microsoft 的朋友:)

Rx 非常惊人。我几乎用它取代了事件。它只是简单美味......

嗨,我是 Rusty,我是一个 Rx 瘾君子......我喜欢它。

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.

当梦初醒 2024-09-11 02:35:24

虽然没有明确内置到 .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.

御守 2024-09-11 02:35:24

如果您愿意使用 .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.

和影子一齐双人舞 2024-09-11 02:35:24

除了提供的答案之外,还可以使用线程池来完成此操作:

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);
}

RegisteredWaitHandle 有一个 Unregister 方法,可用于取消请求

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文