(Win Serv.) It's a long start.



  • I wrote Windows-service, which listens to 25 ports a local car. I've set up a service and started it. введите сюда описание изображения

    The service has been launched for a very long time, and at the end it makes a mistake:

    введите сюда описание изображения

    At the same time, if you enter this service:

    введите сюда описание изображения

    I mean, you can't stop running it, you can't just delete it.

    But at the same time, the service works, it writes logic, the port listens!

    Project code:

    protected override void OnStart(string[] args)
    {
        l.Write("Service started: " + ServiceName);
        EventLog.WriteEntry("Service started: " + ServiceName);
    
    try
    {
        SMTP s = new SMTP(this);
        s.Listen();
    }
    catch (Exception ex)
    {
        l.Write("OnStart error: \n" + ex.ToString());
    }         
    

    }


    This is where we create the juice:

    public void Listen()
    {
    int iter = 0;

    try
    {
        SMTP_Listener = new TcpListener(IPAddress.Any, port);
        SMTP_Listener.Start();
        l.Write("SMTP started " + DateTime.Now.ToShortDateString());
    
        while (true)
        {
            l.Write("iteration: " + iter.ToString());
    
            clientSocket = SMTP_Listener.AcceptSocket();
            string sessionID = clientSocket.GetHashCode().ToString();
    
            l.Write("New session: " + sessionID);
    
            StartProcessing();
        }
    }
    catch (Exception ex)
    {
        l.Write("SMTP Listen Error: " + ex.ToString());
        throw;
    }
    

    }

    void StartProcessing()
    {
    l.Write("StartProcessing() - started");

    try
    {
        string m_ConnectedIp = ParseIP_from_EndPoint(clientSocket.RemoteEndPoint.ToString());
        string m_ConnectedHostName = GetHostName(m_ConnectedIp);
    
        l.Write(String.Format("m_ConnectedIp = {0}, m_ConnectedHostName = {1}", m_ConnectedIp, m_ConnectedHostName));
    
        //РАБОТА С ВХОДНЫМИ ДАННЫМИ
        while (true)
        {
            //если есть данные, то считаем их
            if (clientSocket.Available > 0)
            {
                string lastCmd = ReadLine();
                l.Write("lastCmd: " + lastCmd);
    
                //break; // добавил
    
                //парсим команду
                ProceedCommand(lastCmd);
            }
        }               
    }
    catch (Exception ex)
    {
        l.Write("SMTP StartProcessing Error: " + ex.ToString());
        throw;
    }
    
    l.Write("StartProcessing()  -  end");
    

    }

    I want to make sure that the service can launch normally so that it can be properly stopped from the Windus interface. Maybe it's about endless cycles. Please.



  • Your method Listen Never return the controls - and if the system thinks it's down, we have to get the controls back. OnStart

    You need to create a new one. flow and listen to it.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2