BackgroundService描述說明:
說到定時任務,可能第一個會想到Quartz,但是想到需要更簡潔,而且想要毫秒級的週期,這個Cron真是太不智慧了,雖說可以在單個任務中輪詢,但是這個Trigger用起來是很不適應。那麼找找源頭,有什麼簡單的方法開啓定時任務,然後大佬是這樣說的 幫助 。
看起來很不錯啊,只需要簡單繼承 BackgroundService 基類就可以了,先來做個測試,重寫一下開始、結束、執行:
運行一下試試,很不錯,看起來如此的簡單。
但是爲什麼 停止不了!停止不了!停止不了!,只能求助網上各位大佬了,在此感謝
osc_aq3v6w0z的幫助 原文鏈接,
BackgroundService 中函數調用 ExecuteAsync 時給它賦的參數直接是一個內部的只讀變量,你在外部調用,StartAsync 給它輸入的參數根本就沒有用到。
到底怎麼回事
說到是BackgroundService 基類中並沒有使用傳進去的參數,那麼來看看源碼長什麼樣(開源福利)
// Copyright (c) .NET Foundation. Licensed under the Apache License, Version 2.0.
/// <summary>
/// Base class for implementing a long running <see cref="IHostedService"/>.
/// </summary>
public abstract class BackgroundService : IHostedService, IDisposable
{
private Task _executingTask;
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
public virtual Task StartAsync(CancellationToken cancellationToken)
{
// Store the task we're executing
_executingTask = ExecuteAsync(_stoppingCts.Token);
// If the task is completed then return it,
// this will bubble cancellation and failure to the caller
if (_executingTask.IsCompleted)
{
return _executingTask;
}
// Otherwise it's running
return Task.CompletedTask;
}
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
// Stop called without start
if (_executingTask == null)
{
return;
}
try
{
// Signal cancellation to the executing method
_stoppingCts.Cancel();
}
finally
{
// Wait until the task completes or the stop token triggers
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite,cancellationToken));
}
}
public virtual void Dispose()
{
_stoppingCts.Cancel();
}
}
確實,這就是問題的根源,那麼看看怎麼辦
方法一:跳過StartAsync、StopAsync ,直接調用 ExecuteAsync ;
方法二:仿照官方的 BackgroundService,實現 IHostedService 接口,自己寫一個BackgroundService
方法三:使用 BackgroundWorker
下面來嘗試一下方法二,簡單的件token替換一下
running…