使用QUARTZ.NET启动过程

时间:2018-07-25 08:21:04

标签: c# process cron quartz-scheduler quartz.net

这是我的.NET控制台应用程序program.cs类中的方法

public static void Run()
    {

        var isRunning = false;
        IServiceCollection services = new ServiceCollection();
        Startup startup = new Startup();
        var serviceProvider = startup.ConfigureServices(services);
        IWindsorContainer _container = 
        DateTime thisDay = DateTime.Today;
        while (true)
        {
            if (!isRunning)
            {
                isRunning = true;
                var configuration = _container.Resolve<IConfigurationRoot>();
                var _process = _container.Resolve<Process.Process>();
                var cron = (configuration[Enums.ExecutionTime]);

                if (thisDay == DateTime.Now)
                {
                    try
                    {

                        _process.MainProcess();

                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        isRunning = false;
                        Thread.Sleep(secondsForSleep);
                        _container.Release(_process);
                    }
                }
            }
        }
    }

我想使用quartz.net来启动_process.MainProcess() 但是,我能找到的所有示例都是以启动Jobs开始,有人知道它是否支持启动过程吗?或者是否有解决方法?

1 个答案:

答案 0 :(得分:1)

您只需实现IJob接口并让作业运行该过程:

[DisallowConcurrentExecution]
public class MyProcessJob : IJob
{     
    private readonly Process _process;

    public MyProcessJob(Process process)
    {
        _process = process;
    }

    public void Execute(IJobExecutionContext context)
    {
        _process.MainProcess();
    }
}

Quartz.NET quick start guide中对作业的创建和计划进行了很好的解释。