I've written a little test app that creates an event log entry every few seconds. Problem is, it works fine except that after it creates 4 log entries it stops creating entries. The service does not stop or error. All it does it stop creating entries.
If I restart the service, it works again and creates 4 entries, then stops. It should create entries indefinitely, so I'm not sure what is happening.
Here's the code, I'm wondering if this has to do with some kind of garbage collection:
Jeff W.
MCSE, CNE
If I restart the service, it works again and creates 4 entries, then stops. It should create entries indefinitely, so I'm not sure what is happening.
Here's the code, I'm wondering if this has to do with some kind of garbage collection:
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
namespace EventService
{
public class Service1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private string strELSource = "EventLogTheft";
private string strELLogName = "EventLogTestApp";
private string strELComputerName = ".";
private int iCounter = 0;
public Timer tmr5Seconds = null;
//private Clock myClock;
public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
try
{
//new code
Clock myClock = new Clock();
TimerCallback delTimerMethod = new TimerCallback(timedEvent);
Timer tmr5Secs = new Timer(delTimerMethod, myClock, 0, 5000);
myClock.tmr = tmr5Seconds;
if(!EventLog.SourceExists(strELSource, strELComputerName))
{
Debugger.Break();
Console.WriteLine("Source does not exist. I'll try to create");
//EventLog.CreateEventSource(strELSource, strELLogName, strELComputerName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
}
private void timedEvent(Object o)
{
try
{
Clock myClock = (Clock)o;
iCounter+=1;
EventLog el1 = new EventLog();
el1.Source = strELSource;
el1.Log = strELLogName;
el1.WriteEntry("Timed event. Second: " + iCounter.ToString());
Console.WriteLine(iCounter.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public class Clock
{
public Timer tmr;
}
}
Jeff W.
MCSE, CNE