The code below builds without any errors and ran fine as console application. However now that I've made it into a WindowsService and used the C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil to successfully install it. It stops as soon as I start it up. I feel like I'm missing something simple that I'm going to forever hit myself over the head once another set of eyes looks at it.
Please forgive the mess you are about see. I will clean it up after its working properly. Thanks in advance for any help that can be provided.
Service1.cs
namespace LGCDialHome
{
public partial class Service1 : ServiceBase
{
private System.Timers.Timer _timer = null;
public Service1()
{
InitializeComponent();
System.Timers.Timer stateTimer = new System.Timers.Timer(60000);
}
protected override void OnStart(string[] args)
{
try
{
EventLog.WriteEntry("Dial Home service started : " + DateTime.Now);
_timer = new System.Timers.Timer();
_timer.Interval = 10000; //in milliseconds
EventLog.WriteEntry("Timer Interval is : " + _timer.Interval);
_timer.Elapsed += timer_Elapsed;
_timer.Start();
}
catch (Exception ex)
{
EventLog.WriteEntry("Dial Home service error : " + ex);
}
}
protected override void OnStop()
{
EventLog.WriteEntry("Dial Home service Stopped : " + DateTime.Now);
}
protected void timer_Elapsed(object sender, ElapsedEventArgs e)
{
int invokeCount = 0;
int maxCount = 10;
string host = Environment.MachineName;
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string uname = Environment.UserName;
try
{
_timer.Stop();
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),(++invokeCount).ToString());
Console.WriteLine("Host -> {0} \r\nNTID -> {1}", host, user);
if (invokeCount == maxCount)
{
invokeCount = 0;
}
}
catch (Exception ex)
{
EventLog.WriteEntry("Dial Home service error : " + ex);
}
finally
{
_timer.Start();
}
}
}
}