Problems with Windows Service

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, :blush:)

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();
}
}
 
C

Chris R

DateTime.Now.ToString()
contains ':', which are not allowed in a File. Fix that and you'll be able
to run.

Here's how I create Windows Services:
I create a Windows Service
I create a Windows Application.
I create a Component class with it's own Start, Stop, Pause, Continue,
etc. methods.

I drop the component into the Service and into the Application. On the
application I add 4 buttons, Start, Stop, Pause, and Continue. From both
executables, I'll call these methods on the component. Now I can test all I
want with the Windows application, keeping in mind the Service limitations
and then be able to compile the service with confidence that it has been
well tested.

Take care,
Chris R.

Jesper Stocholm said:
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, :blush:)

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();
}
}



--
Jesper Stocholm
http://stocholm.dk
Give a man a fish and he will have food for a day,
give a man an elephant, and he will have food for a week.
 
J

Jesper Stocholm

Chris R wrote :
DateTime.Now.ToString()
contains ':', which are not allowed in a File. Fix that and you'll be
able to run.

Arrrgh ... :) Thanks a lot - it did the trick.
Here's how I create Windows Services:
I create a Windows Service
I create a Windows Application.
I create a Component class with it's own Start, Stop, Pause,
Continue,
etc. methods.

I drop the component into the Service and into the Application.
On the
application I add 4 buttons, Start, Stop, Pause, and Continue. From
both executables, I'll call these methods on the component. Now I can
test all I want with the Windows application, keeping in mind the
Service limitations and then be able to compile the service with
confidence that it has been well tested.

Thanks for the advice :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top