WebApi的背景工作者

时间:2015-07-17 12:33:36

标签: asp.net-web-api backgroundworker

我试图弄清楚我的API调用请求是如何等待我的后工作者完成的。

这是我的jobManager:

public class JobManager {
public List<AbstractJob> Jobs;

public JobManager() {
  Jobs = new List<AbstractJob>();
}

public int newTestJob() {
  var job = new TestJob();
  job.Name = "TestJob";
  Jobs.Add(job);
  return job.Id;
}

public void startJob(int id) {
  var job = Jobs.Where(j => j.Id == id).FirstOrDefault();
  if (job == null) throw new Exception("not found");

  job.Execute();
}

public string statusJob(int id) {
  var job = Jobs.Where(j => j.Id == id).FirstOrDefault();
  if (job == null) throw new Exception("not found");

  return job.Status;
}

public List<AbstractJob> runningJobs() {
  return Jobs.Where(j => j.Status == "Running").ToList();
}
}

这是一个IJob

public interface IJob {
  void Execute();
}  

这是AbstractJob

public abstract class AbstractJob : IJob {
  static int counter = 0;
  protected BackgroundWorker _bw = new BackgroundWorker();
  public int Id;
  public string Status;
  public string Name;

  public AbstractJob() {
    Interlocked.Increment(ref counter);
    Id = counter;
    _bw.WorkerReportsProgress = true;
    _bw.WorkerSupportsCancellation = true;
    _bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    _bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
    _bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
    _bw.ReportProgress(0, "Idle");
  }

  public virtual string Describe() {
    return "Not much is known about this four legged animal!";
  }

  public void Execute() {
    _bw.RunWorkerAsync();
  }
  private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) {
    this.Status = e.UserState.ToString();
  }

  private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
    BackgroundWorker worker = sender as BackgroundWorker;
    if ((e.Cancelled == true)) {
      this.Status = "Canceled";
    } else if (!(e.Error == null)) {
      this.Status = "Error";

    } else {
      this.Status = "Done";
    }
  }
  public abstract void bw_DoWork(object sender, DoWorkEventArgs e);
}

并说这是我的工作:

public class TestJob : AbstractJob {
  public override void bw_DoWork(object sender, DoWorkEventArgs e) {
    System.Threading.Thread.Sleep(50000); // representing some actual code that takes time
  }
}

所以我想在我的控制器中创建10个作业,并在任务在后台运行时返回ID。

public IEnumerable<int> Get() {
  var ids = new List<int>();
  for (int i = 0; i < 10; i++) {
    var id = jobm.newTestJob();
    jobm.startJob(id);
    ids.Add(id);
  }

  return ids;
}

这一切似乎都很有效,除了请求正在等待作业中的睡眠时间。虽然当我在我的控制器中的return ids上放置一个断点时;它打得非常快(所以异步工作正常)

1 个答案:

答案 0 :(得分:2)

通常你应该有这样的信息: “在异步操作仍处于挂起状态时,异步模块或处理程序已完成。” It is considered as dangerous to launch background tasks without awaiting them

您应该使用TaskParallelLibrary - async / await来做并正确等待所有任务返回:

 public async Task Get()
        {
            var tasks = new List<Task>();
            for (int i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                   //your code here
                }));
            }
            await Task.WhenAll(tasks);
        }

如果您正在使用FireAndForget,则可以使用框架4.5.2中的新QueueBackgroundWorkItem

另请查看此Post。最好使用本机异步方法并将整个链转换为异步,而不是简单地执行“Task.Run”。