Reputation: 1326
now I have a C# application, which runs 24/7 with a timer, which elapse all 30 seconds and do anything.
I want to make this application to a windows service, to run in the background. But the service crash immediately..
My code:
public static System.Timers.Timer _timer = new System.Timers.Timer();
static void Main(string[] args)
{
_timer.Interval = 30000;
_timer.Elapsed += timerCallback;
_timer.AutoReset = true;
_timer.Start();
}
public static void timerCallback(Object sender, System.Timers.ElapsedEventArgs e)
{
// Do anything..
}
And the error:
Windows could not start the Application service on Local Computer.
Error 1053: The service did not respond to the start or control request in a timely fashion
In the windows event viewer this message occured:
A timeout was reached (30000 milliseconds) while waiting for the Application service to connect.
But the error appear faster than 30 seconds?!
Any solutions to run the service?? Thanks
Michael
Upvotes: 0
Views: 881
Reputation: 8902
You could use a Timer to execute the logic periodically within windows service,
protected override void OnStart(string[] args)
{
base.OnStart(args);
Timer timer = new Timer();
timer.Interval = 30*1000;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
//put logic here that needs to be executed for every 30sec
}
Upvotes: 3