Commit 4a534c72 by guanzhenshan

增加定时服务

parent 87e6be37
using System;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;
namespace SkuWHRedundancyService
{
class Program
{
static void Main(string[] args)
static async Task Main(string[] args)
{
Console.WriteLine("统计易仓发货单指标");
var builder = new HostBuilder().ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Services>();
});
var service = new Services();
service.SaveRedundancy(DateTime.Now.AddDays(-1));
await builder.RunConsoleAsync();
}
//static void Main(string[] args)
//{
// var service = new Services();
// service.SaveRedundancy(DateTime.Now.AddDays(-1));
//}
}
}
......@@ -5,12 +5,40 @@ using MySql.Data.MySqlClient;
using Bailun.DC.Models.Warehouse;
using Bailun.DC.Common;
using Dapper;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Extensions.Hosting;
namespace SkuWHRedundancyService
{
public class Services
public class Services : BackgroundService
{
private Timer _timer;
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
return Task.CompletedTask;
}
private void DoWork(object state)
{
try
{
var now = DateTime.Now;
if (now.Hour == 5 && now.Minute == 1)
{
SaveRedundancy(DateTime.Now.AddDays(-1));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public bool SaveRedundancy(DateTime date)
{
......
......@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Dapper" Version="1.60.6" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="MySql.Data" Version="8.0.16" />
</ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment