【发布时间】: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