I wrote a function that creates a permanent timer to execute a function. For example, it calls the desired function every minute. That function has an input parameter which is a number and I want to create it dynamically. For example, zero is passed in the first run and ten units are added to it in each subsequent run.
This is my class :
using Timer = System.Timers.Timer;
public class GetProductsEvent : IHostedService, IDisposable
{
private readonly int _seconds;
private readonly Timer _timer;
private readonly IServiceProvider _serviceProvider;
public GetProductsEvent(IServiceProvider serviceProvider, EventConfiguration eventConfig)
{
_seconds = eventConfig.SendInvoiceTimer;
_timer = new Timer(GetTimePeriod());
_timer.Elapsed += Timer_Elapsed;
_serviceProvider = serviceProvider;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
// var wait = DoWork();
// wait.Wait();
DoWork(0);
}
private void DoWork(int startRow)
{
try
{
using var scope = _serviceProvider.CreateScope();
{
IHostWorker worker =
scope.ServiceProvider.GetRequiredService<IHostWorker>();
worker.GetProductsInfo(startRow);
}
}
catch (Exception)
{
// Logging
}
_timer.Interval = GetTimePeriod();
_timer.Start();
}
private double GetTimePeriod()
{
var runTime = DateTime.Now.AddSeconds(_seconds);
return (runTime - DateTime.Now).TotalMilliseconds;
}
public void Dispose()
{
// TODO release managed resources here
_timer.Stop();
_timer?.Dispose();
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_timer.AutoReset = false;
_timer.Start();
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_timer.Stop();
}
}
The desired function is ‘GetProductsInfo’ and now I don’t know how to change its parameter value