0

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.

JDProwler
  • 109
  • 1
  • 1
  • 8
  • If all you need ist a schedule, why not simply have the Windows Task Scheduler run your App instead of going through all the overhead of a windows service? – Filburt Jul 24 '18 at 12:48
  • You don't need "funny stuff", you need something that keeps running. Start a thread, see [duplicate](https://stackoverflow.com/questions/538925/windows-service-stops-automatically). – CodeCaster Jul 24 '18 at 12:49
  • Basically, what you'll want to do is launch a thread (or more) from OnStart. Then have a queue (or queues) that this thread consumes work from. Your OnCustomCommand handler posts work to the queue. – 500 - Internal Server Error Jul 24 '18 at 12:49
  • You can use asp.net self host web api, MSMQ/RabbitMQ for receiving messages and processing/performing tasks in parallel or sequential. Use SemophoreSlim locking to control the count of parallel tasks. RabbitMQ can also be used to limit number of messages being processed. – Sarvesh Mishra Jul 24 '18 at 12:56
  • Thanks @CodeCaster and the rest of you. I thought I could do this without creating a thread or timer inside of the service. Now I have a better understanding of this. – JDProwler Jul 24 '18 at 13:21

0 Answers0