Good day,
So to start off, I have found a few questions related to this, but not what I'm looking for
- Prevent Windows service manager from stopping a service c#
- Keep a Windows Service running without a timer
What I'm really looking for is a way to keep a service running, without having to place any funny stuff inside it (what's normal to the spider - is chaos to the fly).
So to break down the structure of the system that I am designing, here is some background:
There is a service that runs in the background. This service will hold objects that perform processing at specified intervals. These objects are different types of processes (i.e. Reporting, Processing, etc.) that all launch at different intervals. The idea is that there will be an application that passes values to the service using the ServiceBase.OnCustomCommand(int cmd)
, where these commands will translate into "Pause", "Start", "Restart" and other object queries to determine processing success / completion of a certain queue. So in other words, I am running the "different processors" in parallel, as well as depending on the process, it allows multiple files or tasks to be processed / run in parallel.
I have tried running this service but it stops by itself each time, even if I have the CanStop
property set to false
. In debugging (using a compiler if
) I was able to determine that the service never crashes, but just runs through once, then at the end of the OnStart
method, execution terminates.
So back to the question, how can I allow this service to continue running in the background without having to set up a nonsense timer inside of the service?
Some Code
Service
public override void OnStart(string[] args)
{
try {
double interval = double.Parse(ConfigurationManager.AppSettings["Interval"]);
fileProc = new FileProcessor(interval);
}
catch(Exception ex) {
fileProc = new FileProcessor(180000); // global variable
}
}
File Processor
public FileProcessor(double interval)
{
pauseTimer = null;
runTimer = new System.Timers.Timer()
{
AutoReset = true,
Enabled = true,
Interval = interval
};
runTimer.Elapsed += FileProcessor_OnRun;
OnPause += FileProcessor_OnPause;
runTimer.Start();
IsRunning = true;
}
Thank you in advance for any assistance.