Delay starting Win Service

  • Thread starter Thread starter Rajesh Abraham
  • Start date Start date
R

Rajesh Abraham

I have a Windows Service and in the OnStart, I am
initializing the BusinessLayer Object and calling a method
of the object, which normally takes about 10 mts to finish
execution.

Now when I start the Service from Administrative Tools
services, it shows starting and after sometime throw error
that the service does not respond in time and the ststus
is shown as "starting". However during this time, the
BisObject is doing its job and finally when it is done the
status shows as started.

Is OnStart the right place to call the BisObject. Is there
a better way of doing this. May be a new thread for the
BisObject. In which case, How do I close that thread. What
is the best prctice to follow in this situation.

Thanks,

Rajesh Abraham Chacko
 
As you said, start a new thread from OnStart. You don't need to do anything
special to close the thread, just let it do its job in thread's Start()
method.

Eliyahu
 
Rajesh,

You are on the right track with the thread idea. The OnStart must
finish within the alloted time so you shouldn't put any processing
code there.

I've seen this problem handled two ways. The first and most
straightforward is to instantiate an object in the OnStart and spawn a
new thread using a method of this main object. This will allow the
OnStart to complete in a timely fashion.

The second method is to create a timer object and set it to expire
once with a very short time. You can then put the processing code in
the elapsed event. This will allow the OnStart to complete. Behind
the scenes, the processing of the timer runs on a separate system
thread and because an event raised in a thread will be handled in that
thread no matter in which object it is defined, the elapsed event will
run in the system thread as well. This approach is actually doing the
same thing as the first approach, it is just hiding the threads.

Hope this helps
 
Back
Top