This code: "using cryptotasky.Models; using Microsoft.Extensions.Hosting; namespace…
- User
This code: "using cryptotasky.Models; using Microsoft.Extensions.Hosting;
namespace cryptotasky { public class DailyResetService : IHostedService, IDisposable { private Timer _dailyTimer; private Timer _hourlyTimer;
public System.Threading.Tasks.Task StartAsync(CancellationToken cancellationToken) { var now = DateTime.Now; // Calculate the time until the next desired daily run (12:00:00 AM) var nextDailyRunTime = DateTime.Today.AddDays(1).AddHours(0) - now; // Run the DoWork method every 24 hours _dailyTimer = new Timer(DoWork, null, nextDailyRunTime, TimeSpan.FromDays(1)); // Calculate the time until the next desired hourly run (every hour) var nextHourlyRunTime = DateTime.Now.AddHours(1).Date - now; // Run the PerformActionWhenCloseDateReached method every hour _hourlyTimer = new Timer(PerformActionWhenCloseDateReached, null, nextHourlyRunTime, TimeSpan.FromHours(1)); return System.Threading.Tasks.Task.CompletedTask; } private void DoWork(object state) { using (CryptoTContext db = new CryptoTContext()) { var all = db.Users.ToList(); foreach (var user in all) { user.DailyDoneTasks = 0; } db.SaveChanges(); } } private void PerformActionWhenCloseDateReached(object state) { using (CryptoTContext db = new CryptoTContext()) { var tasks = db.Tasks.Where(t => t.CloseDate != null && t.CloseDate <= DateTime.UtcNow).ToList(); foreach (var task in tasks) { task.CloseDate = null; } db.SaveChanges(); } } public System.Threading.Tasks.Task StopAsync(CancellationToken cancellationToken) { _dailyTimer?.Dispose(); _hourlyTimer?.Dispose(); return System.Threading.Tasks.Task.CompletedTask; } public void Dispose() { _dailyTimer?.Dispose(); _hourlyTimer?.Dispose(); } }}" is causing this error: "Unhandled exception. System.ArgumentOutOfRangeException: Number must be either non-negative and less than or equal to Int32.MaxValue or -1. (Parameter 'dueTime') at System.Threading.Timer..ctor(TimerCallback callback, Object state, TimeSpan dueTime, TimeSpan period) at cryptotasky.DailyResetService.StartAsync(CancellationToken cancellationToken) in C:\Users\kingk\Desktop\Cryptotaskys\cryptotasky\DailyResetService.cs:line 25 at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) at Program.$(String[] args) in C:\Users\kingk\Desktop\Cryptotaskys\cryptotasky\Program.cs:line 63
C:\Users\kingk\Desktop\Cryptotaskys\cryptotasky\bin\Debug\net7.0\cryptotasky.exe (process 13732) exited with code -532462766. Press any key to close this window . . ."
- Assistant
The error is occurring because the
Timerconstructor is being called with an invalid value for thedueTimeparameter.In the code,
nextHourlyRunTimeis calculated and passed as thedueTimeparameter for the_hourlyTimerinstance of theTimerclass. However, the calculation ofnextHourlyRunTimeis incorrect, causing thedueTimeparameter to be negative or greater thanInt32.MaxValue.To fix this issue, you need to double-check the calculation of
nextHourlyRunTime. Make sure it correctly calculates the time until the next desired hourly run.Here's an example of how you can calculate
nextHourlyRunTimecorrectly:var nextHour = now.AddHours(1).Hour; var nextHourlyRunTime = new DateTime(now.Year, now.Month, now.Day, nextHour, 0, 0) - now;Replace the calculation of
nextHourlyRunTimein your code with the above snippet, and the error should be resolved.