J
Jesper Stocholm
I have created a simple service which just copies a fil to a new file
with a new name on certain intervals (the service implements a timer).
I have no problems installing the service and the log-entries I have
created on stop() and start() in the eventlog works fine as well.
However - the actual task is never executed. I catch the execution of
the tick and in the finally-block I write to the eventlog as well. But
only one entry is creates - namely with the first tick of the timer.
The data in eventlog is
The description for Event ID ( 0 ) in Source ( Application ) cannot be
found. The local computer may not have the necessary registry
information or message DLL files to display messages from a remote
computer. The following information is part of the event: Run of
service BackUpMemberData is complete.
The code for the service is included below - can any of you guys see,
what is wrong with it?
Thanks,
)
public class BackupMemberData : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer = null;
private System.ComponentModel.IContainer components;
public BackupMemberData()
{
InitializeComponent();
double interval = 60000;
timer = new System.Timers.Timer(interval);
timer.Elapsed += new ElapsedEventHandler(this.Timer_Tick);
}
private void Timer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
System.Diagnostics.EventLog elog = new System.Diagnostics.EventLog();
elog.Source = "Application";
this.timer.Stop();
string Guid = System.Guid.NewGuid().ToString();
try
{
File.Copy("d:\\testsvc.txt","d:\\testsvc-" + Guid + "-" + DateTime.Now.ToString() + ".txt");
elog.WriteEntry("Backup of member data completed successfully.",EventLogEntryType.Information);
}
catch (System.IO.IOException ioe)
{
elog.WriteEntry(ioe.Message,System.Diagnostics.EventLogEntryType.Error);
}
finally
{
elog.WriteEntry("Run of service BackUpMemberData is complete",System.Diagnostics.EventLogEntryType.SuccessAudit);
}
this.timer.Start();
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new BackupMemberData() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ServiceName = "Backup data (Sekretariat)";
}
protected override void OnStart(string[] args)
{
timer.AutoReset = true;
timer.Enabled=true;
timer.Start();
}
protected override void OnStop()
{
this.timer.AutoReset = false;
this.timer.Enabled = false;
}
protected override void OnPause()
{
this.timer.Stop();
}
protected override void OnContinue()
{
this.timer.Start();
}
}
with a new name on certain intervals (the service implements a timer).
I have no problems installing the service and the log-entries I have
created on stop() and start() in the eventlog works fine as well.
However - the actual task is never executed. I catch the execution of
the tick and in the finally-block I write to the eventlog as well. But
only one entry is creates - namely with the first tick of the timer.
The data in eventlog is
The description for Event ID ( 0 ) in Source ( Application ) cannot be
found. The local computer may not have the necessary registry
information or message DLL files to display messages from a remote
computer. The following information is part of the event: Run of
service BackUpMemberData is complete.
The code for the service is included below - can any of you guys see,
what is wrong with it?
Thanks,

public class BackupMemberData : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer = null;
private System.ComponentModel.IContainer components;
public BackupMemberData()
{
InitializeComponent();
double interval = 60000;
timer = new System.Timers.Timer(interval);
timer.Elapsed += new ElapsedEventHandler(this.Timer_Tick);
}
private void Timer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
System.Diagnostics.EventLog elog = new System.Diagnostics.EventLog();
elog.Source = "Application";
this.timer.Stop();
string Guid = System.Guid.NewGuid().ToString();
try
{
File.Copy("d:\\testsvc.txt","d:\\testsvc-" + Guid + "-" + DateTime.Now.ToString() + ".txt");
elog.WriteEntry("Backup of member data completed successfully.",EventLogEntryType.Information);
}
catch (System.IO.IOException ioe)
{
elog.WriteEntry(ioe.Message,System.Diagnostics.EventLogEntryType.Error);
}
finally
{
elog.WriteEntry("Run of service BackUpMemberData is complete",System.Diagnostics.EventLogEntryType.SuccessAudit);
}
this.timer.Start();
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new BackupMemberData() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ServiceName = "Backup data (Sekretariat)";
}
protected override void OnStart(string[] args)
{
timer.AutoReset = true;
timer.Enabled=true;
timer.Start();
}
protected override void OnStop()
{
this.timer.AutoReset = false;
this.timer.Enabled = false;
}
protected override void OnPause()
{
this.timer.Stop();
}
protected override void OnContinue()
{
this.timer.Start();
}
}