3

I have developed a console app which must run in the background. The app must constantly check a database for new records. If new records are returned they are processed. My app as it currently stands uses while(true) to keep the application running. Is using a while loop the best solution.

A snippet of my code:

static void Main(string[] args)
{
  while(true)
  {
    // Query db for new records
    if(record_count > 0)
    {
      // Process the records
    }
    Thread.Sleep(500);
  }
}
amburnside
  • 1,943
  • 5
  • 24
  • 43
  • I think its a proper soluton – Constantin Treiber Jun 20 '16 at 08:40
  • 4
    You are looking for something in background but using a console app. A console app has a windowed UI (a command console). Please consider using windows services instead as in this link. http://www.csharp-examples.net/create-windows-service/ – RBT Jun 20 '16 at 08:40
  • if the intention is to have it run unattended (without someone looking at the console output), convert to a service. Regarding the Thread.Sleep, consider http://stackoverflow.com/questions/5424667/alternatives-to-thread-sleep – Cee McSharpface Jun 20 '16 at 08:42
  • Did you try googling "best way to keep a C# Console app running"? – Ash Jun 20 '16 at 08:42

5 Answers5

9

Create as Windows Service Application

What a Windows Service is ?

  • Enables you to create long-running executable applications that run in their own windows session.
  • Can be automatically started when the computer boots, can be paused and restarted without any user interaction.
  • Easily installable by running the command line utility InstallUtil.exe and passing the path to the service's executable file.

Why a Windows Service?

One of the most common requirements of some businesses is long-running scheduled jobs based on some time interval. For example: sending a newsletter everyday afternoon or send an email alert to the customer for every one hour.

So building a Windows Service could be one of the reliable solutions to do the goal that can do the required job in the behind the scenes without interfering others users on the same computer.

Please refer the link for step by step implementation -

http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/

Sourav Kumar
  • 296
  • 1
  • 10
  • Noted with thanks But the link contains all required answers. – Sourav Kumar Jun 20 '16 at 08:54
  • 4
    Link only answers can become invalid if that link somehow stops working or gets moved. Adding good enough context to your post always helps the users. – RBT Jun 20 '16 at 09:16
  • Thanks for elaborating on your answer. Fine work! +1 good friend :) –  Jun 20 '16 at 12:08
2

Your approach to keep your console application continuously running is absolutely fine. Only thing is that you want to keep it running in background silently without user to know anything. Since a console app has a windowed UI (a command console) the user will keep seeing it. Here is what you can do to get rid of command console. Since you already have your console application with you, here is what you need to do :

Go to project properties -> Go to application Tab -> Change the Output type to Windows Application

enter image description here

Phew! Now your same console application will run without a command console and your code will keep running.

Creating a windows service will be too much of a kill for such a simple logic. Also installing a windows service is an extra overhead, though I am not aware of the deployment you are looking forward to for your application in production.

RBT
  • 24,161
  • 21
  • 159
  • 240
1

Better option would be to use Windows service that run in background.

Creating a Windows Service Application

Develop and Install a Windows Service in C#

Just google and you can find lots of article and tutorial on Windows Service.

sawbeanraz
  • 187
  • 10
1

There is no need to make your program running as a services. Services aren't for these small tasks. You should simply create a Windows (GUI, WinForms) application with a hidden window. Or, just don't create a window and just keep the Main running. Main would launch a thread and would itself wait for that thread.

A service has to be installed as a service and not all users would have permission to do that. It is complex task to have service, manage it, have security attributes settled et. al.

The hidden application can simply run in current user account (the service would be running in high-privilege SYSTEM or NETWORK account). Your application won't demand any privilege from current user - it just need SQL database/table read access.

Ajay
  • 18,086
  • 12
  • 59
  • 105
-2

Create a windows service which will check if your console app is running. If console app is down, then windows service will execute it again.

Don't write your console app's code into windows service. Because if it will start automatically at windows start up, then it may not work correctly as your console app.

Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36
  • 2
    Why not just do away with the console app entirely and put the processing in the Windows Service? –  Jun 20 '16 at 08:50
  • What will happen if console app crashes? You are creating a control mechanism by doing like that. – Orkun Bekar Jun 20 '16 at 08:53
  • 1
    OP's code has no error handling. Besides, Windows Services allow you to specify recovery options such as _restart service_. _"it may not work correctly"_ - there's no guarantee that it will work either as a console app –  Jun 20 '16 at 08:57
  • 1
    Windows service has a start-up type option `Manual`. This doesn't let the windows service start on its own on OS reboot. – RBT Jun 20 '16 at 09:11