C#---使用Coravel实现定时任务

Coravel是一款框架轻,使用简单,支持秒级定时任务。

1.添加NuGet引用

2.定义自己的工作任务

using Coravel.Invocable;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoravelDemo
{
    public class MyCoravelJob1 : IInvocable
    {
        private readonly ILogger _logger;

        public MyCoravelJob1(ILogger<MyCoravelJob1> logger)
        {
            _logger = logger;
        }

        public async Task Invoke()
        {
            _logger.LogInformation($"Coravel执行了一次{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
        }
    }
}

3.在Startup.cs中注册与配置自己的工作任务

using Coravel;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoravelDemo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //只使用Coravel的任务调度功能
            services.AddScheduler();
            //注册自己的调度任务
            services.AddTransient<MyCoravelJob1>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //配置任务
            var provider = app.ApplicationServices;
            provider.UseScheduler(scheduler =>
            {
                scheduler.Schedule<MyCoravelJob1>()
                //工作日每隔10秒输出
                .EverySeconds(10);
                //只在工作日
                //.Weekday();
            });
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

执行结果:

Coravel的主要方法:

EverySecond()每秒运行一次任务
EveryFiveSeconds()每五秒运行一次任务
EveryTenSeconds()每十秒运行一次任务
EveryFifteenSeconds()每十五秒运行一次任务
EveryThirtySeconds()每三十秒运行一次任务
EverySeconds(3)每 3 秒运行一次任务。
EveryMinute()每分钟运行一次任务
EveryFiveMinutes()每五分钟运行一次任务
EveryTenMinutes()每十分钟运行一次任务
EveryFifteenMinutes()每十五分钟运行一次任务
EveryThirtyMinutes()每三十分钟运行一次任务
Hourly()每小时运行一次任务
HourlyAt(12)在每小时过去 12 分钟运行任务
Daily()每天午夜运行一次任务
DailyAtHour(13)每天下午 1 点 UTC 运行一次任务
DailyAt(13, 30)每天下午 1:30 UTC 运行一次任务
Weekly()每周运行一次任务
Cron("* * * * *")使用 Cron 表达式运行任务

 

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Solovay-Staassen算法是一种用于判断一个大整数是否为素数的算法,其基于Jacobi符号的性质。下面是使用C#实现Solovay-Staassen算法的示例代码: ```csharp using System; using System.Numerics; class SolovayStassen { private static int JacobiSymbol(BigInteger a, BigInteger n) { if (a == 0) return 0; if (a == 1) return 1; int e = 0; while (a % 2 == 0) { a /= 2; e++; } int s; if (e % 2 == 0 || n % 8 == 1 || n % 8 == 7) { s = 1; } else { s = -1; } if (a == 1) return s; if (n % 4 == 3 && a % 4 == 3) { s = -s; } return s * JacobiSymbol(n % a, a); } private static BigInteger ModPow(BigInteger x, BigInteger y, BigInteger m) { BigInteger r = 1; while (y > 0) { if (y % 2 == 1) { r = (r * x) % m; } y /= 2; x = (x * x) % m; } return r; } public static bool IsPrime(BigInteger n, int k) { if (n == 2 || n == 3) { return true; } if (n < 2 || n % 2 == 0) { return false; } for (int i = 0; i < k; i++) { BigInteger a = RandomBigInteger(2, n - 1); BigInteger x = JacobiSymbol(a, n) % n; BigInteger y = ModPow(a, (n - 1) / 2, n); if (x == 0 || y != x) { return false; } } return true; } private static BigInteger RandomBigInteger(BigInteger min, BigInteger max) { Random random = new Random(); int maxBytes = max.ToByteArray().Length; byte[] bytes = new byte[maxBytes]; BigInteger result; do { random.NextBytes(bytes); bytes[bytes.Length - 1] &= (byte)0x7F; // make sure most significant bit is not set result = new BigInteger(bytes); } while (result < min || result > max); return result; } } ``` 该代码中包含以下方法: - `JacobiSymbol(a, n)`:计算Jacobi符号。 - `ModPow(x, y, m)`:计算x的y次方对m取模。 - `IsPrime(n, k)`:使用Solovay-Staassen算法判断n是否为素数,k为测试次数。 - `RandomBigInteger(min, max)`:生成一个介于min和max之间的随机正整数。 要使用该代码,可以通过以下方式调用: ```csharp BigInteger n = new BigInteger("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"); int k = 10; bool isPrime = SolovayStassen.IsPrime(n, k); if (isPrime) { Console.WriteLine(n + " is prime"); } else { Console.WriteLine(n + " is composite"); } ``` 上述代码将使用Solovay-Staassen算法进行10次测试,判断n是否为素数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值