0

I have a Console application that is ran on the background. Its a simple server that built using NetMQ library and NLogger that works locally.

I ran the program using taskScheduler with the following command schtasks /create /sc ONSTART /tn "TheServer" /tr "C:\\Temp\TheServer.exe" /ru System

As you can se, now it will start every time the desktop is turned on and it will run as System (It means that we can see TheServer.exe is running as background through Task Manager)

Question:

My timeline was like the follows:

  1. My PC was on on Friday morning (starting of Business day - 09:00 in the morning)
  2. I didn't turn off my PC on Friday before I go back from work
  3. Monday morning, I checked my PC and TheServer is already gone

Does anyone know why it happens and how to prevent it?


FYI, I write my code as follows

Program.cs

class Program
    {
        [STAThread]
        static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
        
        ...

        static void Main(string[] args)
        {
            try
            {
                var inst = Server.GetInstance();
                inst.Bind();
                inst.Run();

            }
            catch (Exception e)
            {
                log.Error(e.Message);
                log.Error(e.StackTrace);
                throw;
            }
        }
    }

Server.cs

class Server
    {
        private NetMQ.Sockets.ResponseSocket rSocket = new NetMQ.Sockets.ResponseSocket();
        static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

        ...

        public void Bind()
        {
            try
            {
                //bind is only for the Responder or The Server
                rSocket.Bind("tcp://127.0.0.1:32325");
            }
            catch (Exception e)
            {
                log.Error(e.Message);
                log.Error(e.StackTrace);
                throw;
            }
        }

        public void Run()
        {
            while (true)
            {
                 var message = server.ReceiveFrameString();
                 Console.WriteLine("Received {0}", message);
                 // processing the request
                 Thread.Sleep(100);
                 Console.WriteLine("Sending World");
                 server.SendFrame("World");
            }
        }
    }

I wrap my code at Program.cs with try-catch. But nothing written in the log

Then

I tried to find some information at the Window's Event Viewer. But there are no related information why TheServer.exe stopped


Update: Looks like I found what @PMF means. enter image description here

Then one extra question. . .

Is there any way to do that through command prompt?

I tried to read https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks-create Looks like if I didn't set the /du when create the task, it will be forever (?). CMIIW


UPDATE

So yes, we decided to recreate this TheServer.exe as Windows Service. Thanks for your input and opinions.

MH Rahman
  • 81
  • 8
  • 6
    Check the task scheduler settings. There's one setting that would terminate the job after running for more than a specified time (don't know what the default is, though). For what you intend to do, I would recommend using the service management, not the task scheduler. The task scheduler is for short-living processes that need to run once in a while, not for background tasks that should run typically forever. – PMF Apr 12 '22 at 07:19
  • @PMF Thanks for your comment. I think I found what you mean. If task scheduler is not the answer to run the background tasks forever, do you have any idea how to achieve it? – MH Rahman Apr 12 '22 at 07:50
  • 4
    Task scheduler is designed to run short tasks regularly. An application that potentially runs forever is called a service. You could just run it at system startup or when a user logs in or (with .Net Framework) you can create a Windows Service application that Windows itself can manage. https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer – phuzi Apr 12 '22 at 07:56
  • @MHRahman Check phuzi's comment. You should run your application as a _service_ rather than a task. Check out [this question](https://stackoverflow.com/questions/7427395/run-the-console-application-as-service) – PMF Apr 12 '22 at 11:44
  • @phuzi Thanks for your input. Looks like we are planning to migrate our Console Program into Service soon – MH Rahman Apr 13 '22 at 00:05
  • @PMF thanks for the attached link. It looks like I need several references to speed up my migration process – MH Rahman Apr 13 '22 at 00:06

0 Answers0