C# 带有延迟事件通知的作业队列实现

发布于 2024-11-06 09:33:14 字数 377 浏览 1 评论 0原文

我正在寻找 .Net (3.5) 中现有的作业队列实现,我想知道是否有人对一个好的实现有建议。我认为没有作业队列能够完全满足我们的要求,因此我正在寻找一个可以轻松扩展的作业队列。

我们的基本要求:

  1. 需要能够并行或串行运行作业。有些作业可能必须串行完成,有些作业可能能够并行运行,即使这些串行作业正在运行。
    (我想从技术上讲我们可以有 2 个作业队列,但基本实现仍然需要能够处理这两个队列。)
  2. 需要能够让作业触发进度事件 - 但我们希望事件能够在准备好时触发,或者排队然后根据 UI 的轮询触发。
    (我们希望所有事件都在主 GUI 线程中处理,因此在 GUI 更新时我们希望触发自上次更新以来发生的所有事件。)

谢谢,
利隆

I'm looking for an existing job-queue implementation in .Net (3.5) and I was wondering if anyone has suggestions for a good one. I assume that no job queue will be able to do exactly what we want, so I'm looking for one that can easily be extended.

Our basic requirements:

  1. Need to be able to run jobs in parallel or serial. Some jobs might have to be done serially, and some might be able to be run in parallel, even while those serial jobs are being run.

    (I guess technically we could have 2 job queues, but the base implementation still needs to be able to handle both.)
  2. Need to be able to have jobs fire up progress events - but we want to events to be able to fire either when they are ready, or to be queued up and then fired based on a poll from the UI.

    (We want all the events to be processed in the main GUI thread, so on GUI update we are hoping to fire all events that have occurred since the last update.)

Thanks,
Liron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

零度° 2024-11-13 09:33:14

quartz.net 是一个非常全面的作业调度程序

quartz.net is a pretty comprehensive job scheduler

一世旳自豪 2024-11-13 09:33:14

我已经做了一些类似的事情,它实际上只是一个消息队列,所以这里有一些关于基本实现的注释:

  1. 创建一个类,其中包含您的作业类型的一些队列(队列可能是一个不错的选择),一个线程,一个 AutoResetEvent、一个 QueueJob 方法和一个事件(例如“HandleJob”或类似的事件)
  2. 启动线程。线程应该等待该事件,然后处理队列中的所有作业。对于每个作业,只需触发 HandleJob 事件即可。如果您在 UI 线程上需要它,只需让您的事件处理程序使用 Invoke 或 BeginInvoke。最后,清理队列。
  3. 调用 QueueJob 时,将作业添加到队列中并发出事件信号。
    • 注意:您需要确保队列访问同步

此外,我认为应该并行运行的作业实际上并不需要是作业 - 您可以在线程池上执行它们。

从这里,您可以设计定时执行(例如,应该在 10 秒后运行的作业)、迭代器的执行以及对公开 AsyncWaitHandler 的作业的支持(例如,允许其他作业在等待某些内容时执行)。

I've done some similar things, and it's really just basically a message queue, so here are some notes on a basic implementation:

  1. Create a class that contains some queue of your job type (Queue might be a fine choice), a Thread, an AutoResetEvent, a QueueJob method, and an event (say, "HandleJob" or something like that)
  2. Start up the thread. The thread should wait on the event, and then process all jobs in the queue. For each job, just fire the HandleJob event. If you need it on the UI thread, just have your event handler use Invoke or BeginInvoke. Finally, clear the queue.
  3. When QueueJob is called, add the job to the queue and signal the event.
    • Note: you'll want to make sure queue access is synchronized

Also, I think that jobs that should be run in parallel don't really need to be jobs - you can execute them on the ThreadPool.

From here, you can craft in timed execution (say, a job that should run 10 seconds later), execution of iterators, and support for jobs that expose an AsyncWaitHandler (e.g. allow other jobs to execute while it's waiting for something).

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