Service Starting Problem!
-
I have created a windows service to send email on daily basis. It has a timer object that fires an event after 24 hours,
private System.Timers.Timer timer = new System.Timers.Timer (86400000);
In timer event I do my working i.e. I send my emails, something like this:
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
eventLog1.WriteEntry("Calling web service for Sending Reminders on " + DateTime.Now.ToLongDateString());
if (EmailObj.SendReminder()) // if sending email is sussful
{
eventLog1.WriteEntry("The operation was successfully completed on " + DateTime.Now.ToLongDateString());;
}
else
{
eventLog1.WriteEntry("There was some error, the operation failed on " + DateTime.Now.ToLongDateString());;
}if (DateTime.Today.Day == 1) { // first date of month; send emails }
}
The problem is, I have installed the service on Windows server 2003, but the service starts and stops immediately with the message “The MyServiceName service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service” I can see the event log entries that I did on OnStart event of the service; also the service is working fine on Windows XP. Please help me I have to run this service on MS Windows server 2003 forever to send emails on daily basis,
cout << "\0"; // its backSlashZero
-
I have created a windows service to send email on daily basis. It has a timer object that fires an event after 24 hours,
private System.Timers.Timer timer = new System.Timers.Timer (86400000);
In timer event I do my working i.e. I send my emails, something like this:
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
eventLog1.WriteEntry("Calling web service for Sending Reminders on " + DateTime.Now.ToLongDateString());
if (EmailObj.SendReminder()) // if sending email is sussful
{
eventLog1.WriteEntry("The operation was successfully completed on " + DateTime.Now.ToLongDateString());;
}
else
{
eventLog1.WriteEntry("There was some error, the operation failed on " + DateTime.Now.ToLongDateString());;
}if (DateTime.Today.Day == 1) { // first date of month; send emails }
}
The problem is, I have installed the service on Windows server 2003, but the service starts and stops immediately with the message “The MyServiceName service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service” I can see the event log entries that I did on OnStart event of the service; also the service is working fine on Windows XP. Please help me I have to run this service on MS Windows server 2003 forever to send emails on daily basis,
cout << "\0"; // its backSlashZero
What is there in your
OnStart()
method ? Is your timer enabled ?backSlashZero wrote:
private System.Timers.Timer timer = new System.Timers.Timer (86400000);
You are keeping the thread sleeping for such a long time. Better method to do this is to write a console application which sends mail and exits automatically. Use windows scheduler to schedule this exe to run at required time. So you are not keeping any threads sleeping for long time. Windows service should be used, if it has to do processing all time when it runs, not for scheduling like this, AFAIK.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
I have created a windows service to send email on daily basis. It has a timer object that fires an event after 24 hours,
private System.Timers.Timer timer = new System.Timers.Timer (86400000);
In timer event I do my working i.e. I send my emails, something like this:
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
eventLog1.WriteEntry("Calling web service for Sending Reminders on " + DateTime.Now.ToLongDateString());
if (EmailObj.SendReminder()) // if sending email is sussful
{
eventLog1.WriteEntry("The operation was successfully completed on " + DateTime.Now.ToLongDateString());;
}
else
{
eventLog1.WriteEntry("There was some error, the operation failed on " + DateTime.Now.ToLongDateString());;
}if (DateTime.Today.Day == 1) { // first date of month; send emails }
}
The problem is, I have installed the service on Windows server 2003, but the service starts and stops immediately with the message “The MyServiceName service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service” I can see the event log entries that I did on OnStart event of the service; also the service is working fine on Windows XP. Please help me I have to run this service on MS Windows server 2003 forever to send emails on daily basis,
cout << "\0"; // its backSlashZero
Add a keep-alive thread that doesn't do anything but sleeps for 4.5 seconds.
protected void KeepAliveThread()
{
ServiceController controller = new ServiceController(this.ServiceName);
while (true)
{
if (controller.Status == ServiceControllerStatus.Running)
{
Thread.Sleep(4500);
}
}
}In your OnStart() method, do this:
Thread thread = new Thread(new ThreadStart(KeepAliveThread));
thread.Start();"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Add a keep-alive thread that doesn't do anything but sleeps for 4.5 seconds.
protected void KeepAliveThread()
{
ServiceController controller = new ServiceController(this.ServiceName);
while (true)
{
if (controller.Status == ServiceControllerStatus.Running)
{
Thread.Sleep(4500);
}
}
}In your OnStart() method, do this:
Thread thread = new Thread(new ThreadStart(KeepAliveThread));
thread.Start();"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Thanks! I tried it out but problem is still there :confused:
cout << "\0"; // its backSlashZero
-
What is there in your
OnStart()
method ? Is your timer enabled ?backSlashZero wrote:
private System.Timers.Timer timer = new System.Timers.Timer (86400000);
You are keeping the thread sleeping for such a long time. Better method to do this is to write a console application which sends mail and exits automatically. Use windows scheduler to schedule this exe to run at required time. So you are not keeping any threads sleeping for long time. Windows service should be used, if it has to do processing all time when it runs, not for scheduling like this, AFAIK.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
N a v a n e e t h wrote:
Better method to do this is to write a console application which sends mail and exits automatically. Use windows scheduler to schedule this exe to run at required time.
Thanks I am going to do this now :~
cout << "\0"; // its backSlashZero
-
Thanks! I tried it out but problem is still there :confused:
cout << "\0"; // its backSlashZero
Are you doing the following in OnStart()?
timer.Enabled = true; timer.Start();
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Are you doing the following in OnStart()?
timer.Enabled = true; timer.Start();
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Yes i did!
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart: Calling web service for Sending Reminders on "
+ DateTime.Now.ToLongDateString());EmailObj.SendReminder(); \\\\send reminders if any! timer.Enabled = true; timer.Start();
}
cout << "\0"; // its backSlashZero