hangfire使用

发布时间 2024-01-09 19:38:30作者: 虎虎生威啊

# hangfire使用

using System;
using Hangfire;
using Hangfire.SqlServer;
using Hangfire.Storage.SQLite;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main()
        {
            GlobalConfiguration.Configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
                .UseColouredConsoleLogProvider()
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseSQLiteStorage("Data Source=hangfire.db");


            // 队列,执行一次
            BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));

            // 
            BackgroundJob.Schedule(
                () => Console.WriteLine("Hello, world 10s"),
                TimeSpan.FromSeconds(10));

            RecurringJob.AddOrUpdate("easyjob", () => Console.Write("Easy!"), Cron.Minutely);


            using (var server = new BackgroundJobServer())
            {
                Console.ReadLine();
            }
        }
    }
}