'error 1503 the service didn't respond to the start or control request in a timely fashion

I have installed a Windows Service for my project, this error pop out when I start the service.

error 1503 the service didn't respond to the start or control request in a timely fashion

However, this project works fine when I debug in Visual Studio Code but when I use Visual Studio 2017 to create and start the service by following this tutorial and

Few solution I tried but the error still the same. Here are the solution I have tried.

Use CCCleaner to scan and fix issues

Modify ServicesPipeTimeout to 180000

The Logservice below can write the String into a text file, where I analyze the service run up until which part then fails. The service only able to run until LogService("3"); then it fails at receive the bytes from port.

Here is the code:

 public Service1()
        {
            InitializeComponent();
            LogService("server");

        try
        {
            LogService("1");
            IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
            UdpClient udpListener = new UdpClient(514);
            byte[] bReceive; string sReceive; string sourceIP;
            Console.WriteLine("Waiting...");
            /* Main Loop */
            /* Listen for incoming data on udp port 514 (default for SysLog events) */
            while (true)
            {
                LogService("2");
                try
                {
                    LogService("3");
                    bReceive = udpListener.Receive(ref anyIP);
                    LogService("4");
                    /* Convert incoming data from bytes to ASCII */
                    sReceive = Encoding.ASCII.GetString(bReceive);
                    LogService(sReceive);
                    /* Get the IP of the device sending the syslog */
                    sourceIP = anyIP.Address.ToString();
                    LogService("5");
                    LogService(sourceIP);
                    new Thread(new logHandler(sourceIP, sReceive).handleLog).Start();
                    /* Start a new thread to handle received syslog event */

                    LogService(sReceive);
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            throw ex;
        }
    }

I'm not sure if the error is occur because of the codes or other reasons

Update Refer to this post, I tried to turn off the firewall to receive all the connection, but the error still remain the same.

There was once my project successfully listen the data from the server after I add udpListener.Client.Bind(anyIP); into the code, but then after some modification it is not functioning again. I'm not sure is the Bind() make the code works even just for once.



Solution 1:[1]

This is the way I solve my error refer to this post. Anyway, I'm not completely understand the process behind this, if anyone have some good example or link in explaining this solution please don't hesitate to comment below.

protected override void OnStart(string[] args)
    {
        LogService("Service started");
        NewThread = new Thread(runSysLog);
        NewThread.Start();
    }

    protected override void OnStop()
    {
        LogService("Service stopped");
        StopRequest.Set();
        //NewThread.Join();
    }

public void runSysLog()
    {
        try
        {
            AutoResetEvent StopRequest = new AutoResetEvent(false);
            /* Main Loop */
            while (true)
            {
                if (StopRequest.WaitOne(5000)) return;
                try
                { 
                    //while (udpListener.Available > 0)
                    if (udpListener.Available > 0)
                    {
                        //Some code here  
                    }
                }
                catch (Exception ex)
                {
                    LogService("Whileloop exception: " +ex.ToString());
                    throw ex;
                }
            }
        }
        catch (Exception ex)
        {
            LogService("Run Sys Log Exception: " +ex.ToString());
            throw ex;
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JJ___