My service is running but it does not do any of the logic i inserted in OnStart, Also, I am try to log messages to the eventviewer but it does not even get to that part (which i assume is falling before the OnStart..) Can someone please tell me what i am doing wrong? Thanks,
public partial class DSGService : ServiceBase
{
private static string ftpPath = ConfigurationManager.AppSettings["FtpPath"];
private Timer _timer;
private bool _isRunning;
public DSGService()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
WriteToEventViewer("error!:" + e.ExceptionObject.ToString());
}
OnStart logic
protected override void OnStart(string[] args)
{
_isRunning = false;
_timer = new Timer
{
AutoReset = true,
Interval = 5000,
};
_timer.Elapsed += new ElapsedEventHandler(this.TimerElapsed);
_timer.Start();
}
code to do things
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
if (_isRunning)
{
WriteToEventViewer("isrunning is true");
return;
}
try
{
_isRunning = true;
WriteToEventViewer("started");
//do things..
WriteToEventViewer("generated!");
}
}
finally
{
_isRunning = false;
WriteToEventViewer("done");
}
}
private void GenerateAndPublishData(ServerData server)
{
try
{
//do things...
}
catch (Exception ee)
{
WriteToEventViewer(string.Format("error: {0}", ee);
}
}
protected override void OnStop()
{
_timer.Stop();
_timer.Dispose();
_timer = null;
}
write to eventviewer
private void WriteToEventViewer(string msg)
{
using (EventLog eventLog = new EventLog("DSGService"))
{
eventLog.Source = "DSGService";
eventLog.WriteEntry(msg, EventLogEntryType.Information, 101, 1);
}
}
}```