Windows service timeout

  • Thread starter oscar.acostamontesde
  • Start date
O

oscar.acostamontesde

Hello everybody. I'm currently developing a windows service. In OnStart
a launch two threads that carry on some work while the service is
running. My problem comes from the fact that once the service is
installed, it just time out at system startup and don't start. But i'm
able to start it manually once i'm logged in. I read in a page
something about signed assemblyes, saying that clr have to verify the
executable at startup, causing the delay. I just unsigned the assembly
and still face the same problem. I also notice that some compilation of
the solution start at system starup, and othes does not, with out
aparent reason, because there is no code change. Any sugestion? Here is
my code for OnStart method.

/// <summary>
/// Method launched on service start.
/// </summary>
/// <param name="args">none</param>
protected override void OnStart(string[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(JobWs),
null);
ThreadPool.QueueUserWorkItem(new
WaitCallback(JobManteinance), null);
//// Write info to eventlog, so controler detects the
service start.
eventLog.WriteEntry("Service started",
EventLogEntryType.Information);
}

JobWs and JobManteinance are two functions that do all the work.
Thanks in advance.
 
P

Phil Wilson

There's a general question about how you are installing the service and how
you are starting it. For example, it has a dependency on the Eventlog
service, so there's a chance it will fail when it starts up after a reboot
unless it's configured to be dependent on the Event Log service. There may
be other services it's dependent on (such as RPC). You're probably crashing
in OnStart and you time out because you never return properly from it.

Your model isn't one I'd use. OnStart is like an event that lets you do some
initialization. It's typically not the place to start threads that do all
the work on the service, IMO. Usually the Main method is where you use
System.ServiceProcess.ServiceBase to call run on an instance of your service
class. You should also worry about putting something in OnStop to shut down
your threads in an orderly manner.
 
O

oscar.acostamontesde

Hello Phil, thanks for your answer. So, you suggest that I only shoul
initialize variables in OnStart method? And how do I start the threads
then? Could you provide some code example of the model you talk about?
Another question is: If i do my service dependant on other, windows
will launch first the necesary services before launching mine?
It is the first windows service i write and in the examples i saw in
internet people usually start the threads in OnStart event. I'm also
overwriting the OnStop event handler, an there I stop the threads I
launched. As they are not cooperating in any way, it's not important
the order. Any help would be apreciated. Thanks again.

Oscar Acosta

Phil said:
There's a general question about how you are installing the service and how
you are starting it. For example, it has a dependency on the Eventlog
service, so there's a chance it will fail when it starts up after a reboot
unless it's configured to be dependent on the Event Log service. There may
be other services it's dependent on (such as RPC). You're probably crashing
in OnStart and you time out because you never return properly from it.

Your model isn't one I'd use. OnStart is like an event that lets you do some
initialization. It's typically not the place to start threads that do all
the work on the service, IMO. Usually the Main method is where you use
System.ServiceProcess.ServiceBase to call run on an instance of your service
class. You should also worry about putting something in OnStop to shut down
your threads in an orderly manner.

--
Phil Wilson [MVP Windows Installer]
----
Hello everybody. I'm currently developing a windows service. In OnStart
a launch two threads that carry on some work while the service is
running. My problem comes from the fact that once the service is
installed, it just time out at system startup and don't start. But i'm
able to start it manually once i'm logged in. I read in a page
something about signed assemblyes, saying that clr have to verify the
executable at startup, causing the delay. I just unsigned the assembly
and still face the same problem. I also notice that some compilation of
the solution start at system starup, and othes does not, with out
aparent reason, because there is no code change. Any sugestion? Here is
my code for OnStart method.

/// <summary>
/// Method launched on service start.
/// </summary>
/// <param name="args">none</param>
protected override void OnStart(string[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(JobWs),
null);
ThreadPool.QueueUserWorkItem(new
WaitCallback(JobManteinance), null);
//// Write info to eventlog, so controler detects the
service start.
eventLog.WriteEntry("Service started",
EventLogEntryType.Information);
}

JobWs and JobManteinance are two functions that do all the work.
Thanks in advance.
 
P

Phil Wilson

If you set up the dependencies correctly then yes, your service will start
after the event log service has started.

There are always lots of ways to do something, but this is the general model
for services that I prefer:
http://www.codeproject.com/csharp/cron.asp

where a thread belonging to the class is started in OnStart and stopped in
OnStop, and the thread runs some method you implement. However it's cleaner
to use a ManualResetEvent that the thread checks periodically so that it can
shut down cleanly, rather than Thread.Abort used there. Your ThreadPool
doesn't seem to deal with the case where the service gets stopped (how do
you shut down the threads etc). So you might get into some issues when you
try to stop your service unless you deal with that. However I suspect your
crashing problem at boot time is your dependency on the event log service.
--
Phil Wilson [MVP Windows Installer]
----
Hello Phil, thanks for your answer. So, you suggest that I only shoul
initialize variables in OnStart method? And how do I start the threads
then? Could you provide some code example of the model you talk about?
Another question is: If i do my service dependant on other, windows
will launch first the necesary services before launching mine?
It is the first windows service i write and in the examples i saw in
internet people usually start the threads in OnStart event. I'm also
overwriting the OnStop event handler, an there I stop the threads I
launched. As they are not cooperating in any way, it's not important
the order. Any help would be apreciated. Thanks again.

Oscar Acosta

Phil said:
There's a general question about how you are installing the service and
how
you are starting it. For example, it has a dependency on the Eventlog
service, so there's a chance it will fail when it starts up after a
reboot
unless it's configured to be dependent on the Event Log service. There
may
be other services it's dependent on (such as RPC). You're probably
crashing
in OnStart and you time out because you never return properly from it.

Your model isn't one I'd use. OnStart is like an event that lets you do
some
initialization. It's typically not the place to start threads that do all
the work on the service, IMO. Usually the Main method is where you use
System.ServiceProcess.ServiceBase to call run on an instance of your
service
class. You should also worry about putting something in OnStop to shut
down
your threads in an orderly manner.

--
Phil Wilson [MVP Windows Installer]
----
Hello everybody. I'm currently developing a windows service. In OnStart
a launch two threads that carry on some work while the service is
running. My problem comes from the fact that once the service is
installed, it just time out at system startup and don't start. But i'm
able to start it manually once i'm logged in. I read in a page
something about signed assemblyes, saying that clr have to verify the
executable at startup, causing the delay. I just unsigned the assembly
and still face the same problem. I also notice that some compilation of
the solution start at system starup, and othes does not, with out
aparent reason, because there is no code change. Any sugestion? Here is
my code for OnStart method.

/// <summary>
/// Method launched on service start.
/// </summary>
/// <param name="args">none</param>
protected override void OnStart(string[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(JobWs),
null);
ThreadPool.QueueUserWorkItem(new
WaitCallback(JobManteinance), null);
//// Write info to eventlog, so controler detects the
service start.
eventLog.WriteEntry("Service started",
EventLogEntryType.Information);
}

JobWs and JobManteinance are two functions that do all the work.
Thanks in advance.
 
O

oscar.acostamontesde

Hello Phil. Thanks a lot, you were completely rigth about it, I added
the dependency and the service now starts fine!
I was using the ThreadPool because I was getting mad about the reason
for the service not starting. So I thougt
that maybe the overhead caused by the new threads were the problem, and
I decided to use the threadpool thinking that maybe as those threads
were already created by the framework i could win some miliseconds ;)
Thanks a lot again!
Oscar Acosta

Phil said:
If you set up the dependencies correctly then yes, your service will start
after the event log service has started.

There are always lots of ways to do something, but this is the general model
for services that I prefer:
http://www.codeproject.com/csharp/cron.asp

where a thread belonging to the class is started in OnStart and stopped in
OnStop, and the thread runs some method you implement. However it's cleaner
to use a ManualResetEvent that the thread checks periodically so that it can
shut down cleanly, rather than Thread.Abort used there. Your ThreadPool
doesn't seem to deal with the case where the service gets stopped (how do
you shut down the threads etc). So you might get into some issues when you
try to stop your service unless you deal with that. However I suspect your
crashing problem at boot time is your dependency on the event log service.
--
Phil Wilson [MVP Windows Installer]
----
Hello Phil, thanks for your answer. So, you suggest that I only shoul
initialize variables in OnStart method? And how do I start the threads
then? Could you provide some code example of the model you talk about?
Another question is: If i do my service dependant on other, windows
will launch first the necesary services before launching mine?
It is the first windows service i write and in the examples i saw in
internet people usually start the threads in OnStart event. I'm also
overwriting the OnStop event handler, an there I stop the threads I
launched. As they are not cooperating in any way, it's not important
the order. Any help would be apreciated. Thanks again.

Oscar Acosta

Phil said:
There's a general question about how you are installing the service and
how
you are starting it. For example, it has a dependency on the Eventlog
service, so there's a chance it will fail when it starts up after a
reboot
unless it's configured to be dependent on the Event Log service. There
may
be other services it's dependent on (such as RPC). You're probably
crashing
in OnStart and you time out because you never return properly from it.

Your model isn't one I'd use. OnStart is like an event that lets you do
some
initialization. It's typically not the place to start threads that do all
the work on the service, IMO. Usually the Main method is where you use
System.ServiceProcess.ServiceBase to call run on an instance of your
service
class. You should also worry about putting something in OnStop to shut
down
your threads in an orderly manner.

--
Phil Wilson [MVP Windows Installer]
----
Hello everybody. I'm currently developing a windows service. In OnStart
a launch two threads that carry on some work while the service is
running. My problem comes from the fact that once the service is
installed, it just time out at system startup and don't start. But i'm
able to start it manually once i'm logged in. I read in a page
something about signed assemblyes, saying that clr have to verify the
executable at startup, causing the delay. I just unsigned the assembly
and still face the same problem. I also notice that some compilation of
the solution start at system starup, and othes does not, with out
aparent reason, because there is no code change. Any sugestion? Here is
my code for OnStart method.

/// <summary>
/// Method launched on service start.
/// </summary>
/// <param name="args">none</param>
protected override void OnStart(string[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(JobWs),
null);
ThreadPool.QueueUserWorkItem(new
WaitCallback(JobManteinance), null);
//// Write info to eventlog, so controler detects the
service start.
eventLog.WriteEntry("Service started",
EventLogEntryType.Information);
}

JobWs and JobManteinance are two functions that do all the work.
Thanks in advance.
 
O

oscar.acostamontesde

Hello again. I have one more question. Is there any way to know all the
services that mine depends on?
Thanks,

Oscar Acosta

Phil said:
If you set up the dependencies correctly then yes, your service will start
after the event log service has started.

There are always lots of ways to do something, but this is the general model
for services that I prefer:
http://www.codeproject.com/csharp/cron.asp

where a thread belonging to the class is started in OnStart and stopped in
OnStop, and the thread runs some method you implement. However it's cleaner
to use a ManualResetEvent that the thread checks periodically so that it can
shut down cleanly, rather than Thread.Abort used there. Your ThreadPool
doesn't seem to deal with the case where the service gets stopped (how do
you shut down the threads etc). So you might get into some issues when you
try to stop your service unless you deal with that. However I suspect your
crashing problem at boot time is your dependency on the event log service.
--
Phil Wilson [MVP Windows Installer]
----
Hello Phil, thanks for your answer. So, you suggest that I only shoul
initialize variables in OnStart method? And how do I start the threads
then? Could you provide some code example of the model you talk about?
Another question is: If i do my service dependant on other, windows
will launch first the necesary services before launching mine?
It is the first windows service i write and in the examples i saw in
internet people usually start the threads in OnStart event. I'm also
overwriting the OnStop event handler, an there I stop the threads I
launched. As they are not cooperating in any way, it's not important
the order. Any help would be apreciated. Thanks again.

Oscar Acosta

Phil said:
There's a general question about how you are installing the service and
how
you are starting it. For example, it has a dependency on the Eventlog
service, so there's a chance it will fail when it starts up after a
reboot
unless it's configured to be dependent on the Event Log service. There
may
be other services it's dependent on (such as RPC). You're probably
crashing
in OnStart and you time out because you never return properly from it.

Your model isn't one I'd use. OnStart is like an event that lets you do
some
initialization. It's typically not the place to start threads that do all
the work on the service, IMO. Usually the Main method is where you use
System.ServiceProcess.ServiceBase to call run on an instance of your
service
class. You should also worry about putting something in OnStop to shut
down
your threads in an orderly manner.

--
Phil Wilson [MVP Windows Installer]
----
Hello everybody. I'm currently developing a windows service. In OnStart
a launch two threads that carry on some work while the service is
running. My problem comes from the fact that once the service is
installed, it just time out at system startup and don't start. But i'm
able to start it manually once i'm logged in. I read in a page
something about signed assemblyes, saying that clr have to verify the
executable at startup, causing the delay. I just unsigned the assembly
and still face the same problem. I also notice that some compilation of
the solution start at system starup, and othes does not, with out
aparent reason, because there is no code change. Any sugestion? Here is
my code for OnStart method.

/// <summary>
/// Method launched on service start.
/// </summary>
/// <param name="args">none</param>
protected override void OnStart(string[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(JobWs),
null);
ThreadPool.QueueUserWorkItem(new
WaitCallback(JobManteinance), null);
//// Write info to eventlog, so controler detects the
service start.
eventLog.WriteEntry("Service started",
EventLogEntryType.Information);
}

JobWs and JobManteinance are two functions that do all the work.
Thanks in advance.
 

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