ASP.NET
  Home arrow ASP.NET arrow Page 5 - Managing Windows Services with Visual Stud...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Managing Windows Services with Visual Studio 2005
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-01-03

    Table of Contents:
  • Managing Windows Services with Visual Studio 2005
  • Properties of the Service
  • Handling Loss of Service
  • Programmatic access using the NET framework
  • Starting, Pausing and Stopping a Service

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Managing Windows Services with Visual Studio 2005 - Starting, Pausing and Stopping a Service


    (Page 5 of 5 )

    Service Status

    These actions can be managed by using the methods of the Service Controller class. Some services have only starting and stopping while others may have the option of being paused. You may verify if the service has stopped or started by looking at its status property. This has a number of values given by their "enums" as shown in the next two pictures.

     

     

    Stopping and Starting

    The code shown in the next paragraph shows how you may stop and start the service whose name is OracleXEClrAgent programmatically.

    Private Sub Button3_Click(ByVal sender As System.Object,_
    ByVal e As System.EventArgs) Handles Button3.Click
    Dim mysvcCntrl As New ServiceProcess.ServiceController
    mysvcCntrl.ServiceName = "OracleXEClrAgent"
    MessageBox.Show(mysvcCntrl.Status)
    Try
    If mysvcCntrl.Status = 4 Then
    mysvcCntrl.Stop()
    'does not refresh in the services panel
    mysvcCntrl.Refresh()
    MessageBox.Show(mysvcCntrl.Status)
    End If
    Catch ex As Exception
    MessageBox.Show(ex.InnerException.ToString)
    End Try
    End Sub
    

    In the above code the program will stop the service if it is running. If it is already stopped it may throw up an exception. When a value of 1 is used instead of 4 for status in the above code, you will get the following message about the exception.

    The Service Controller's other properties can also be used by using the same code. You may look up the status codes, or you can alternatively use the syntax shown in the next paragraph.

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim mysvcCntrl As New ServiceProcess.ServiceController
    mysvcCntrl.ServiceName = "MSSQL$MYSORIAN"
    With mysvcCntrl
    MessageBox.Show(.DisplayName)
    MessageBox.Show(.MachineName)
    MessageBox.Show(.ServiceHandle.ToString)
    End With
    MessageBox.Show(mysvcCntrl.status)
    If (mysvcCntrl.Status = ServiceControllerStatus.Stopped) _
    Then
    MessageBox.Show("Starting the SQL Server")
    Try
    mysvcCntrl.Start()
    mysvcCntrl. _
    WaitForStatus(ServiceControllerStatus.Running)
    MessageBox.Show(mysvcCntrl.Status)
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End If
    End Sub

    After stopping or starting the service, the Service Control Manager's screen in the Control Panel does not get refreshed. To see if the change has taken place you need to refresh the view. Also to get the correct service name you should look up the property of the service in the services window, or use the code shown earlier which lists the name of the services.

    Also make sure that the form on which all these various buttons are placed have the following "imports" statements.

    Imports System.ServiceProcess.ServiceController
    Imports System.ServiceProcess
    

    Summary

    This tutorial described how to manage the Windows service using the Control Panel in the Windows machine as well as programmatically using the appropriate namespace class in the Visual Studio 2005 IDE.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · For those using Windows OS one of the important places they should get used to is...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT