Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / June 2005

Tip: Looking for answers? Try searching our database.

Service OnStart: return or not?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lucvdv - 14 Jun 2005 10:19 GMT
Something I can't quite get out of the docs, is what the 'normal' way is to
implement a service's main body.

My service collects data from network-attached devices at regular
intervals, so it's basically an endless loop with a Thread.Sleep() for
timing.

Do you start a worker thread in OnStart and then return from the function
while the thread remains running to do the work?

Or can you put the main execution loop in OnStart and return from it only
after OnStop has been called?

So far I've tried only the thread method and works
- OnStart creates and starts the thread
- OnStop calls Thread.Abort() on it
- Cleanup is done in the Finally section of a Try block around the loop

The service runs until the thread is stopped, but I was wondering if I
might be creating a thread I don't need.
Stefan Simek - 14 Jun 2005 10:54 GMT
Hi,

While the OnStart method is running, the service is in "starting" state.
You definitively have to return from it, as the service manager will
kill your process if it doesn't manage to return from OnStart in 90
seconds or so.

As for the Thread.Abort(), maybe a better idea would be to implement the
shutdown mechanism using a volatile bool flag. See

http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation

for why using Thread.Abort() might not be the best solution.

HTH,
Stefan

> Something I can't quite get out of the docs, is what the 'normal' way is to
> implement a service's main body.
[quoted text clipped - 16 lines]
> The service runs until the thread is stopped, but I was wondering if I
> might be creating a thread I don't need.
Lucvdv - 14 Jun 2005 16:50 GMT
> Hi,
>
> While the OnStart method is running, the service is in "starting" state.
> You definitively have to return from it, as the service manager will
> kill your process if it doesn't manage to return from OnStart in 90
> seconds or so.

Thanks.  That's exactly what I needed/wanted to hear.

> As for the Thread.Abort(), maybe a better idea would be to implement the
> shutdown mechanism using a volatile bool flag. See

Not much of an issue in this case, but thanks for the warning.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.