如何运行 只同时做一个作业?

How to run only one job concurrently?

我有一台 hangfire 服务器,有大约 50 个重复作业。像 this example 中那样在 IIS 上设置 Hangfire。 在 startup.cs 中添加到 hangfire 的重复作业如下:

RecurringJob.AddOrUpdate(job.desctiprion,
            () => job.startJob(), 
            job.cron, 
            TimeZoneInfo.Local);

我需要每分钟 运行 添加新的循环作业。但是这项工作几乎不可能 运行 分钟。我的目标 - 提供兼职此工作的唯一范例。

有哪些解决方案?据我了解,需要一个线程队列之类的东西,但队列不支持线程设置。

您可以使用 DisableConcurrentExecution 过滤器来防止并发执行。

[DisableConcurrentExecution(timeoutInSeconds: 60)]
public void SomeJob()
{
  //method body
}

This filter places a distributed lock in the beginning of a method performance and releases it after it was completed using the IServerFilter interface. The type and method name are being used as a locking resource. Each storage uses its own distributed lock implementation. (read more here)

 var worker = new BackgroundWorker();
 worker.DoWork += worker_DoWork;
 worker.RunWorkerCompleted += worker_RunWorkerCompleted;
 private void worker_DoWork(object sender, DoWorkEventArgs e)
 {
        // do your task here
 }
 private void worker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
 {

       //Do when completed 
 }