使用hangfire启动控制台应用程序过程

时间:2018-07-25 05:51:27

标签: c# .net console-application hangfire recurring-events

我有program.cs类,我从_process.MainProcess()类中调用Process.cs。但是我想每天早上5点运行此过程。

我试图这样做:

class Program
{
    public static IConfigurationRoot Configuration { get; }
    static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();


        return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(config)
            .CaptureStartupErrors(true)
            .PreferHostingUrls(false)
            .Configure(app =>
            {
                RunIBIT();
            })
            .Build();
    }

    public static void RunIBIT()
    {
        var isRunning = false;
        IServiceCollection services = new ServiceCollection();
        Startup startup = new Startup();
        var serviceProvider = startup.ConfigureServices(services);
        GlobalConfiguration.Configuration.UsePostgreSqlStorage("Host=;Port=;Database=;Username=;Password=");
        while (true)
        {
            if (!isRunning)
            {
                isRunning = true;
                var configuration = _container.Resolve<IConfigurationRoot>();
                var _process = _container.Resolve<Process.Process>();

                int secondsForSleep = Convert.ToUInt16(configuration[Enums.SleepTime]);
                using (var server = new BackgroundJobServer())
                {
                    RecurringJob.AddOrUpdate(
                        () => _process.MainProcess(),
                        "0 0 5 * * ?");
                }
                try
                {
                   // _process.MainProcess();
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    isRunning = false;
                    Thread.Sleep(secondsForSleep);
                    _container.Release(_process);
                }

            }
        }
    }
}

但是 RecurringJob.AddOrUpdate(() => _process.MainProcess(), "0 0 5 * * ?"); 没有启动此过程。

我还尝试添加BackgroundJob.Schedule( () => _process.MainProcess(), TimeSpan.FromSeconds(10)); 只是为了检查是否将执行后台进程而不是RecurringJob(因为TimeStamp不适合我的情况)但是也没有启动。有谁知道是什么原因以及如何启动它?

0 个答案:

没有答案