I want to set a Timer to a method, so its called every, say, 10 minutes, while the application is running.
I have read up on it, but I haven't found an example, where it seems to have the same configuration/start up settings and the time tutorials mostly work with a main method, so I haven't figured out yet, which services I have to add to my startup and/or where to put my time and method.
So I would set my timer like here:
var stateTimer = new Timer(MyMethod, null, 1000, 600000); //any way to change miliseconds to minutes here?
and then write my method like:
static void MyMethod
{
//all the code I want to execute every 10 minutes
}
This is my Startup:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
//....
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//...
}
}
}
So where do I put the time and the method? If someone has an example using the same structure of startup, etc. it would be greatly appreciated. I have found for example this, but it's different from my setup.