hi i am having a problem in running a long task for my background service using the IHostedService at first it really work fine but in the long run the background service suddenly stopped with this thread exited code:
(嗨,我在使用IHostedService为我的后台服务运行长时间任务时遇到问题,它确实可以正常工作,但从长远来看,后台服务因此线程退出代码而突然停止:)
The thread 10824 has exited with code 0 (0x0).
The thread 12340 has exited with code 0 (0x0).
The thread 9324 has exited with code 0 (0x0).
The thread 11168 has exited with code 0 (0x0).
The thread 11616 has exited with code 0 (0x0).
The thread 9792 has exited with code 0 (0x0).
i register my background service as
(我将后台服务注册为)
//Register Background
serviceCollection.AddSingleton<CoinPairBackgroundService>
serviceCollection.AddSingleton<SaveFakePersonBackgroundService>();
serviceCollection.AddSingleton<LeaderboardMinutesBackgroundService>();
serviceCollection.AddSingleton<LeaderboardHoursBackgroundService>();
serviceCollection.AddSingleton<IHostedService, CoinPairBackgroundService>();
serviceCollection.AddSingleton<IHostedService, SaveFakePersonBackgroundService>();
serviceCollection.AddSingleton<IHostedService, LeaderboardMinutesBackgroundService>();
serviceCollection.AddSingleton<IHostedService, LeaderboardHoursBackgroundService>();
because in the future i want to manually turned on and off my background services with use of
(因为将来我想使用手动打开和关闭后台服务)
IServiceProvider provider = _serviceProvider.GetService<MyBackgroundServiceHere>();
provider.StartAsync();
provider.StopAsync
and this was my code in my background services StartAsync
(这是我在后台服务StartAsync中的代码)
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogDebug("Leaderboard minute ranking updates is starting");
Task.Run(async () =>
{
while (!cancellationToken.IsCancellationRequested)
{
_logger.LogDebug("Leaderboard minute ranking updates is dequeueing");
await DequeueRandomCustomers();
_logger.LogDebug("Leaderboard minute ranking updates is enqueueing");
await EnqueueRandomCustomers();
_logger.LogDebug("Leaderboard minute ranking updates thread is now sleeping");
//sleep for 1 minute
await Task.Delay(new TimeSpan(0, 0, 10));
}
});
return Task.CompletedTask;
}
I am confuse if there are some problems with registering my background service because i see my background service starts but in the long run it suddenly stopped i already get rid of those Thread.Sleep() in my background services hope you can help thanks in advance.
(如果我注册后台服务时遇到一些问题,我会感到困惑,因为我看到我的后台服务启动了,但是从长远来看,它突然停止了,我已经摆脱了后台服务中的那些Thread.Sleep()希望您能提前帮忙谢谢。)
ask by James Tubiano translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…