一、概述
通过继承 BackgroundJob<TArgs> 类或者直接实现 IBackgroundJob<TArgs> 接口,我们可以创建一个后台作业。
二、使用
2.1定义后台任务
public class TestJob : BackgroundJob<int>, ITransientDependency
{
public override void Execute(int number)
{
Logger.Debug(number.ToString());
}
}
2.1调用后台任务
public class MyService
{
private readonly IBackgroundJobManager _backgroundJobManager;
public MyService(IBackgroundJobManager backgroundJobManager)
{
_backgroundJobManager = backgroundJobManager;
}
public void Test()
{
_backgroundJobManager.Enqueue<TestJob, int>(42);
}
}
网友评论