I am trying to achieve two seperate System.Timers.Timer
in WebApi project .(I knew its bad practice to use timers in web api but due to restriction of not using .EXE based app trying to achieve using web api in .NET)
Below is the API Application start method in which I have declared two timers and started them using Timer.Start()
method.but both timers eventHandlerFunction are not calling as expected.
Expected behaviour is
FetchTimer should start as soon as API starts.(will be fetching data from client API and inserting into db) HSMSentTimer also should start parallely as soon as API starts.(fetching data from db and will be sending to another api based on interval followed by some business logic)
What I tried is :
public class WebApiApplication : System.Web.HttpApplication
{
public static LogBO _globalObjects = new LogBO(WebConfigurationManager.AppSettings["LogFileDrive"], WebConfigurationManager.AppSettings["LogFolderName"]);
public Timer FetchTimer;
public Timer HSMSentTimer;
public static DAO_ dao = new DAO_();
public static SqlConnection sqlConnection;
protected void Application_Start()
{
FetchTimer = new Timer();
FetchTimer.Elapsed = new ElapsedEventHandler(FetchTimerHandler);
FetchTimer.Interval = 3000;
FetchTimer.AutoReset = false;
FetchTimer.Start();
WriteLog.WriteInfoLog(_globalObjects, "..............", ".......................");
HSMSentTimer = new Timer();
HSMSentTimer.Elapsed = new ElapsedEventHandler(HSMSentTimerHandler);
HSMSentTimer.Interval = 6000;
FetchTimer.AutoReset = false;
HSMSentTimer.Start();
}
public void FetchTimerHandler(object sender, ElapsedEventArgs eventArgs)
{
FetchTimer.Stop();
WriteLog.WriteInfoLog(_globalObjects, "FetchTimerHandler()", "FetchTimerHandler started..... ");
}
public void HSMSentTimerHandler(object sender,ElapsedEventArgs eventArgs)
{
HSMSentTimer.Stop();
WriteLog.WriteInfoLog(_globalObjects, "HSMSentTimerHandler()", "HSMSent timer started..... ");
}
}
OUTPUT : .............. | .......................
FetchTimerHandler() | FetchTimerHandler started.....
Thank you!
CodePudding user response:
I had created static class and put the code inside the static class and then i just called it before the app.Run() method. It fired both the events and shown both timer values in console.
info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5246 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: C:\Users\30462\source\repos\EventSourcingTimer\EventSourcingTimer\ Fetch Timer:3000 HSM Timer 6000
CodePudding user response:
I have fixed the issue by adding slight delay to second timer so it get some gap while firing itself after firing fetch timer.
FetchTimer = new Timer();
FetchTimer.Elapsed = new ElapsedEventHandler(FetchTimerHandler);
FetchTimer.AutoReset = true;
FetchTimer.Start();
HSMSentTimer = new Timer();
HSMSentTimer.Elapsed = new ElapsedEventHandler(HSMSentTimerHandler);
HSMSentTimer.Interval = 200;
HSMSentTimer.AutoReset = true;
HSMSentTimer.Start();