Yasskier
Yasskier

Reputation: 811

"Always running" console application

I wrote a console application that is currently running on the server. It doesn't require any user input (other than parameters at start which can be done via start parameter).

Unfortunately this solution is bad, because someone can accidentally turn it off (i.e. when connecting to server using Remote Desktop Connection and then logging off instead simply disconnecting). I need it to run all the time.

One solution would be to turn it into windows service, but so far using SC or third-party tools like nssm or RunAsService failed (SC and Nssm create a service but such service cannot be started).

I could completely rewrite my program to be a proper service... but to be honest I'm struggling with it (and from what I've read its not recommended practice).

Finally I could leave it as a console app and use task scheduler to run it -which does look like a decent solution, but (like I've mentioned) I need it to run all the time (it can be turned off and on - very short downtimes are not an issue).

Could I please ask for any help with setting such task?

SOLVED

After few attempts I've turned it into service using Topshelf andthis great guide.

Upvotes: 2

Views: 3673

Answers (3)

Aron
Aron

Reputation: 15772

There are two methods you can use to run a .net program constantly in windows. Both have advantages and disadvantages.

Windows Service

  • Recommended Solution
  • Will startup service on computer start (doesn't require someone to log on)
  • Has some (limited) error handling in the form of restarts
  • Good for very reliable services that can run for long periods of time
  • Service handles its own state
  • Can easily crash due to memory leaks

IIS Application Server

  • Not recommended solution
  • Starts with windows, but might not start your application
  • Requires newer windows to allow always on configuration
  • Always on configuration is complicated
  • State is handled by IIS
  • Much better resiliency to crappy programming, as IIS will restart for you
  • IIS will also likely kill your threads for you (so your scheduler will stop working)

I suspect the reason you were told that a windows service is not recommended was due to the fact that it could crash due to memory leaks. But that issue will occur no matter what, since your program needs to run for a long time (its not a problem with windows services, but long lived processes).

Upvotes: 4

ANewGuyInTown
ANewGuyInTown

Reputation: 6467

If you don't want to re-write your console app into a windows service and want it to be running all the time, the only solution I could see is:

  • Create a small window's service, that checks to see if your console process is running or not.

  • If it finds that there is no Console process, then start a new one.

    Process[] pname = Process.GetProcessesByName("YourConsoleApp.exe");
    if (pname.Length == 0)
       Process.Start("YourConsole.exe")
    else
        //Do nothing 
    

Upvotes: 0

Mike Beeler
Mike Beeler

Reputation: 4101

There are a number of rules that need to be followed to write a functional windows service including but not limited to

  • the ability to complete the initialization process in a specific time
  • a general understanding of threads

There is nothing inherently bad about writing a windows service they just require more effort and an installer.

Based on your description a scheduled job seems to fit your requirements

Upvotes: 0

Related Questions