The Windows service stops and starts immediately, but it should not

I create a Windows service, and after installing the service, it stops and starts immediately, but it should not be at all. I used to get errors that the service did not respond to the start command in a timely manner, so I took the init code and put it on the stream, and now I'm here:

protected override void OnStart(string[] args)
{
    this.EventLog.WriteEntry("ATNotifier Started");

    ThreadPool.QueueUserWorkItem(WaitOnEmailsChanged);
    ThreadPool.QueueUserWorkItem(Init, "IP");
}

In the waitonemailschanged stream, a file system manager is simply created to see if the settings file (XML document) will be modified, and whether data is loaded from this file if this happens. So far, it just waits endlessly (this is a common case, since it will only change several times a year), since no changes are made to the XML document.

The Init stream uses all kinds of things, including creating and running the System.Timers.Timer object, the Elapsed method is the meat of the service.

I can’t understand why it will start and then stop immediately. I should also note that eventviewer does not show logs from this application.

edit> I tried to create the β€œcorrect” threads with the same results, and I deleted everything except creating and starting a timer, like this:

protected override void OnStart(string[] args)
{
    this.EventLog.WriteEntry("ATNotifier Started");

    m_Timer = new System.Timers.Timer(90000.0); // 1.5 mins
    m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed);

    m_Timer.Start();
}

and I still get the same message. It is almost as if OnStart was never called.

+3
source share
5 answers

, EventLog.WriteEntry , EventSource. . http://msdn.microsoft.com/en-us/library/xzwc042w.aspx

+2

, .

+3

, , . ?

    m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed);
    m_Timer = new System.Timers.Timer(90000.0); // 1.5 mins

?

+3

, , - OnStart, , , .

, , IIRC Service, OnStart:

base.OnStart(args);
+1
source

ThreadPool are thread threads; they will not continue the process. I suspect you need the "right" thread ...

Try: new Thread(SomeCode).Start();or similar.

0
source

Source: https://habr.com/ru/post/1703382/


All Articles