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 / General / December 2005

Tip: Looking for answers? Try searching our database.

Service question in c# .Net 2.0 framework

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Benj - 12 Dec 2005 17:26 GMT
I am looking for a way to cleanly stop my service in the event of an error.  
Within the worker thread, if an unrecoverable error is caught, I would like
to stop my service.  Exiting the worker thread doesn't seem to do it.  Using
the SCM to stop the service doesn't feel right.  Am I missing something?  My
service looks like (unrunnable example code below):

public partial class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
    ListenerWorkerThread = new Thread(new ThreadStart(PortListener));
    ListenerWorkerThread.Start();
}
protected void PortListener()
{
    try
    {
     //do some work
    }
    catch (Exception ex) //some really bad error
    {
       //Write it to the log and stop the service cleanly
    }
}
}
Kevin Yu [MSFT] - 13 Dec 2005 06:09 GMT
Hi Ben,

In .NET you can use ServiceController class to start or stop a service.
ServiceController.Stop will stop this service and any services that are
dependent on this service. Please check the following link for more
information.

http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicecontro
ller.stop.aspx

Kevin Yu
Signature

=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Benj - 13 Dec 2005 18:37 GMT
Figured this out.  Just needed:

this.stop();
Kevin Yu [MSFT] - 14 Dec 2005 02:35 GMT
Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
Signature

=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


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



©2009 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.