Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys,

I have problem with my WindowsService.
I took the example program from Codeproject and added my Code:
Creating a Basic Windows Service in C#[^]

I also created another class, with which I write data to my SQL Server.
But this should happen only at a particular time, so I created a Timer to read every second the system time.

Now I want to start the Timer with the OnStart Function of my Service, but when I start the Service this error appears:

Service "AS400 duplicate" was started on "Local Computer" and then stopped. Some services stop automatically if they are not used by other services or programs.
(translated from german)

C#
protected override void OnStart(string[] args)
{
AS400duplicate ASdb = new AS400duplicate(); //initialise class
ASdb.StartTimer(); //start Timer
}


I hope you can help me!

Best regards
Tom
Posted
Updated 3-May-12 3:03am
v2

It fails because you created the AS400dupicate object in the scope of the OnStart method. When OnStart returns, the object is no longer in scope and is destroyed, therby destroying your timer.

Declare the AS400duplicate variable outside the OnStart method...

By the way, AS400duplicate is a terrible class name.
 
Share this answer
 
Comments
Michael Bookatz 3-May-12 11:34am    
Why is AS400duplicate a bad class name? Maybe it's referring to an IBM AS 400 computer that is the duplicate of the main running system?
Dave Kreskowiak 3-May-12 11:53am    
Maybe...

Thanks for proving my point! You have no idea what it really represents, do you?
Michael Bookatz 3-May-12 12:32pm    
I don't know the domain so may make sense in the domain. Plus maybe the asker changed it as it is commercially sensitive so used another name.
Member 7983150 3-May-12 13:16pm    
Hi Dave,

I declared it Global but it still doesn´t work.
Same error.
By the way, it´s really an IBM AS400 in my company and I have to duplicate data from it and put it on the SQL-Server...
Dave Kreskowiak 3-May-12 13:38pm    
"AS400duplicate" tells me that it should be some kind of exception or other state message that says a record or other object has been duplicated, possibly when it should not have been.

A better name would be "As400DataDuplicator", but even then, that name suggests a closed design dedicated to an inflexible purpose (being that it only works with an AS400.

I'm not going to get into the gritty details of how I think it should be designed and what the class names should be as we getting off topic here.


The point of your question is why your Timer isn't keeping your service alive. Without seeing the relevent code it's very difficult to say what's going wrong.
I found the solution!

I only had to intialize the Timer like this:

//Timer
public void startTimer()
{
Timer timer1 = new Timer();
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}

Now it is possible to call the function...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900