Kept on encountering this error message when starting my Windows Service.
The service on local computer started and then stopped. Some services stop automatically if they are not in use by other services and programs.
My codes:
protected override void OnStart(string[] args)
{
string eventLogMessage = string.Format(@"Notify Service is starting :{0}", DateTime.Now);
EventLogging.LogInformation(eventLogMessage);
double interval;
try
{
interval = Convert.ToDouble(ConfigurationManager.AppSettings["intervalInSeconds"]);
EventLogging.LogInformation(
string.Format("Loaded configuration: Interval duration is {0} minutes", (interval / 60)));
}
catch (Exception exception)
{
interval = 3600;
eventLogMessage = string.Format("Loading configuration failed: Interval duration is {0} minutes", (interval / 60));
eventLogMessage += string.Format("\nMessage was: {0}", exception.Message);
EventLogging.LogWarning(eventLogMessage);
}
interval = interval * 1000;
_timer.Interval = interval;
_timer.Elapsed += TimerTick;
_timer.Start();
eventLogMessage = string.Format(@"Notify service has started: {0}", DateTime.Now);
EventLogging.LogInformation(eventLogMessage);
var workerThread = new Thread(NotifyUsers) { IsBackground = true };
workerThread.Start();
}
private void NotifyUsers()
{
var userBL = new UserBL();
List<User> usersToBeMailed = userBL.GetAllUsersWhosePasswordsWillExpire();
string eventLogMessage = string.Format("Number of users to be mailed is {0}", usersToBeMailed.Count);
EventLogging.LogInformation(eventLogMessage);
foreach (User user in usersToBeMailed)
{
userBL.MailUser(user);
}
}
private void TimerTick(object sender, ElapsedEventArgs e)
{
var workerThread = new Thread(NotifyUsers) { IsBackground = true };
workerThread.Start();
}
protected override void OnStop()
{
base.OnStop();
string eventLogMessage = @"Password notify service has stopped: " + DateTime.Now;
EventLogging.LogInformation(eventLogMessage);
}
protected override void OnPause()
{
base.OnPause();
_timer.Stop();
EventLogging.LogWarning("Paused");
}
protected override void OnContinue()
{
base.OnContinue();
_timer.Start();
EventLogging.LogInformation("Resumed");
}
}
You have to have at least one of your threads doing something and not being a background thread, to keep the process alive. The thread that the OnStart
code runs on isn't really one of your threads, and besides, you have to return from OnStart
in order to be considered to have successfully started.
This could be as simple as creating and running a new Thread
from your OnStart
method that just waits on a ManualResetEvent
object that you Set
from your OnStop
code.
Alternatively, find some useful work for this thread to do (but still use the event object to signal when it should shut down), or alternative two is - consider whether this code belongs in a service at all. If it's just waking periodically to notify users, why not use a scheduled task instead?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.