Эта функция имеет входной параметр, который является числом, и я хочу создать его динамически. Например, при первом запуске передается ноль, а при каждом последующем к нему добавляется десять единиц.
Это мой класс:
Код: Выделить всё
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();
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();
}
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... -parameter