【问题标题】:Shutdown console application after quartz schedule石英时间表后关闭控制台应用程序
【发布时间】:2020-02-11 15:15:59
【问题描述】:

我想知道如何在 Quartz 计划完成后关闭我的控制台应用程序。

var schedular = await GetScheduler();
await schedular.Start();

await scheduler.ScheduleJob(GetActivityReportDetailJob(), GetSimpleTrigger(nameof(ActivityReportDetailJob)));

Thread.Sleep(2000);
await scheduler.Shutdown();
Environment.Exit(0);

第一个问题是我不能使用 ContinueWith() 来调用,所以我求助于使用 Thread.Sleep()。我不知道如何让 Quartz 返回除任务之外的任何其他内容。但是,当到达 Environment.Exit(0) 时。我明白了:

等待主机被释放。确保所有“IHost”实例都包含在“使用”块中。

编辑:

我的主要:

static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureHostConfiguration(configHost =>
                {
                    configHost.SetBasePath(Directory.GetCurrentDirectory());
                    configHost.AddJsonFile(Constants.Hostsettings, optional: true);
                    configHost.AddEnvironmentVariables(prefix: Constants.Prefix);
                    configHost.AddCommandLine(args);
                })
                .ConfigureAppConfiguration((hostContext, configApp) =>
                {
                    configApp.SetBasePath(Directory.GetCurrentDirectory());
                    configApp.AddJsonFile(Constants.Appsettings, optional: true);
                    configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
                    configApp.AddEnvironmentVariables(prefix: Constants.Prefix);
                    configApp.AddCommandLine(args);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddLogging();
                    services.AddHostedService<PollingService>();
                })
                .ConfigureLogging((hostContext, configLogging) =>
                {
                    configLogging.AddConsole();
                })
                .UseConsoleLifetime()
                .Build();

            try
            {
                await host.RunAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

我不确定这个 using 语句应该放在哪里。

【问题讨论】:

    标签: c# console-application quartz.net


    【解决方案1】:

    尝试从main方法的main函数返回语句

    static void Main(string[] args)
    {
        while (true)
        {            
            return; //This will exit the console application's running thread
        }
    }
    

    System.Environment.Exit(-1);

    【讨论】:

    • 刚刚添加了我的主要问题。
    • host.RunAsync() 需要使用 await 吗? ?
    • 老实说不确定,这是我创建“服务”的第一个项目。
    • 你可以删除和使用像 try { await host.RunAsync(); 这样的代码。 } 捕捉(异常前) { Console.WriteLine(ex); }
    猜你喜欢
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多