I create this Windows service by following the instructions in MSDN Firmware: creating a Windows service and after a successful installation, I go to the .msc services to start the Windows service, and before it finishes launching, I get the following message:
The EIWindowsService service on the local computer started and then stopped. Some services automatically stop if they are not used by other services or programs.
I know that the Windows service starts normally, because there is an entry in the log file indicating that the service is running. I did some research before posting here and responding with “Some service stops automatically” that the problem may be that the OnStart method is causing an error or that OnStart is not starting the thread. So I changed my code, so the only thing OnStart has is to start two timers and write to the log, so exception handling is not required. I also added a thread to switch to another method.
I tried the Windows service again, and I know that it "moved" to the new method that the stream points to, because I had a log entry that had an aFormatException error due to some conversion I was doing. I commented on the conversion, and the Windows service was still just starting to start, and then automatically stopped.
Further research showed me that I might need a loop to save the processing in the method, so I took the information from C - Windows Service Services and installed it until the endless while loop. I also discovered that there might be a Garbage Collection, and set the KeepAlive statement for timers, as shown in the Examples section of MSDN Class Timer. All the same problems.
, , , . , , - Windows, #. , , , . , , Windows, , Windows.
, , . 15 , .
SERVICE1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
namespace EIWindowsService
{
public partial class Service1 : ServiceBase
{
Logs.ErrorLog logFile = new Logs.ErrorLog();
private System.Threading.Thread onStartThread;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
iTimer.Start();
iTimer.Elapsed += new ElapsedEventHandler(iTimer_Elapsed);
pTimer.Start();
pTimer.Elapsed += new ElapsedEventHandler(pTimer_Elapsed);
onStartThread = new System.Threading.Thread(TimerValue);
onStartThread.Start();
logFile.SendToLog("EIWindows Service started on " + GetDate());
}
catch (ArgumentOutOfRangeException ex)
{
logFile.SendToLog("ArgumentOutOfRangeException", "EIWindowsService\\Service1.cs", "OnStart()", ex);
}
}
protected override void OnStop()
{
iTimer.Stop();
pTimer.Stop();
logFile.SendToLog("EIWindowsService\\Service1.cs", "OnStop()", "EIWindows Service stopped on " + GetDate());
}
private void TimerValue()
{
try
{
while (1 > 0)
{
GC.KeepAlive(iTimer);
GC.KeepAlive(pTimer);
}
}
catch (OverflowException ex)
{
logFile.SendToLog("OverflowException", "EIWindowsService\\Service1.cs", "TimerValue()", ex);
}
catch (ArgumentException ex)
{
logFile.SendToLog("ArgumentException", "EIWindowsService\\Service1.cs", "TimerValue()", ex);
}
catch (FormatException ex)
{
logFile.SendToLog("FormatException", "EIWindowsService\\Service1.cs", "TimerValue()", ex);
}
}
private string GetDate()
{
string current = "No Date Recorded";
try
{
current = DateTime.Now.ToString("F");
}
catch (FormatException ex)
{
logFile.SendToLog("FormatException", "EIWindowsService\\Service1.cs", "GetDate()", ex);
}
return current;
}
private void iTimer_Elapsed(object source, ElapsedEventArgs e)
{
try
{
iTimer.Stop();
ImportI();
iTimer.Start();
}
catch (ArgumentOutOfRangeException ex)
{
logFile.SendToLog("ArgumentOutOfRangeException", "EIWindowsService\\Service1.cs", "iTimer_Elapsed()", ex);
}
}
private void pTimer_Elapsed(object source, ElapsedEventArgs e)
{
try
{
pTimer.Stop();
ImportP();
pTimer.Start();
}
catch (ArgumentOutOfRangeException ex)
{
logFile.SendToLog("ArgumentOutOfRangeException", "EIWindowsService\\Service1.cs", "pTimer_Elapsed()", ex);
}
}
private void ImportI()
{
}
private void ImportP()
{
}
}
}
SERVICE1.DESIGNER.CS (, relavant)
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pTimer = new System.Timers.Timer(10800000);
this.iTimer = new System.Timers.Timer(3600000);
this.pTimer.Enabled = true;
this.iTimer.Enabled = true;
this.ServiceName = "EIWindowsService";
}
#endregion
private System.Timers.Timer pTimer;
private System.Timers.Timer iTimer;