VB.NET - start window service automatically not working

Asked By kiruba .e on 20-Jun-12 09:31 AM
Earn up to 50 extra points for answering this tough question.
hi,

i wrote this code in my windowservice.


Its not working. what s my ques is, how to write code to start window service after my setup file installation. 

I dont want to use installuitl or anything. just i want to install the service only.  After installation it should start automatically.

 Private Sub ServiceInstaller1_Committed(ByVal sender As Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles ServiceInstaller1.Committed
      Dim controller = New ServiceController(Me.ServiceInstaller1.ServiceName)
      controller.Start()
      controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running)
    End Sub


thanks regards
kiruba.e
[)ia6l0 iii replied to kiruba .e on 20-Jun-12 12:16 PM
Please use the AfterInstall method, this works for me.  For your code sample, it will be as follows:

Private Sub ServiceInstaller1_AfterInstall(sender As Object, e As InstallEventArgs)
Dim sctlr As New ServiceController(Me.ServiceInstaller1.ServiceName)
sctlr.Start()
End Sub


If you still see it not started, look at the Event Viewer - there would be an error logged on why the service failed to initialize.  (Please type "eventvwr" in your run menu without quotes, to launch the event viewer. Please look below for a screen shot)



Hope this helps.
Jitendra Faye replied to kiruba .e on 21-Jun-12 12:41 AM
For this you need to add installer class and in that class you have to define some functionality like this-

Public ServiceInstaller()
{
    //... Installer code here
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
   //Code to start automatically 
    ServiceController sc = new ServiceController("ServiceName");
    sc.Start();
}

Try this and let me know;

TSN ... replied to kiruba .e on 21-Jun-12 12:44 AM

In your installer class just add the event handler for the afterinstall ,

public ServiceInstaller()

{

    //... Installer code here

    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);

}

 

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)

{

    ServiceController sc = new ServiceController("ServiceName");

    sc.Start();

}

kiruba .e replied to TSN ... on 21-Jun-12 03:25 AM
as im using vb.net it shows error. can u give that in vb.net pls.
TSN ... replied to kiruba .e on 21-Jun-12 05:19 AM

hi…

here is the code in VB.Net


Public Sub New()

        '... Installer code here

        Me.AfterInstall += New InstallEventHandler(AddressOf ServiceInstaller_AfterInstall)

End Sub

Private Sub ServiceInstaller_AfterInstall(sender As Object, e As InstallEventArgs)

        Dim sc As New ServiceController("ServiceName")

        sc.Start()

End Sub

Vikram Singh Saini replied to kiruba .e on 21-Jun-12 11:42 PM
Along with other tech guys suggestions, I would recommend you to follow steps:

1. Open service class design window and add installer for service.
2. Right click serviceInstaller's instance serviceInstaller1 (by default) --> Properties
3. Set StartType (Indicates how and when this service is started) to Automatic.
4. Build and install the service. Now onwards the service would start automatically after installation.



help
http: / / techinterviewquestion.blogspot.com / blog. it is working fine. i used public void StartService(string ServiceName) { ServiceController sc = new ServiceController(ServiceName); if (sc ! = null && sc.Status = = ServiceControllerStatus.Stopped) { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); } } ASP.NET Discussions ServiceControllerStatus (1) ServiceController (1) WaitForStatus (1) ASP.NET (1) StartService (1) Status (1 ServiceName (1) Stopped (1) This is the ASP.NET newsgroup - do you have an issue with from techinterviewquestion.blogspot.com / blog. it is working fine. i used public void StartService(string ServiceName) { ServiceCont
Oct-12 08:25 PM I have the following code at the backend void StopService(serviceName){ try{ ServiceController svc = new ServiceController(serviceName); svc.Stop(); svc.WaitForStatus(ServiceControllerStatus.Stopped); } } catch (Exception){ } My Questions are: (1) I am using svc.WaitForStatus(ServiceControllerStatus.Stopped); (no time out defined here, coz I don't know how long the service description: ServiceController WaitForStatus() method query I have the following code at the backend void StopService(serviceName){ try{ ServiceController svc = n
services = ServiceController.GetServices(); / / try to find service name foreach (ServiceController service in services) { if (service.ServiceName = = serviceName) return true ; } return false ; thank you serviceController class gives the info. . . ServiceController[] services = ServiceController.GetServices you want to check the running services only or else skip this if (service.Status = = ServiceControllerStatus.Running) { if (service. ServiceName = = "Name of the Service") { Console.WriteLine("Found. . ."); } } } This one lists only services but not device
services = ServiceController.GetServices(machinename); for(int i = 0;i< services.Length;i++) { if (services[i].ServiceName = = servicename) { ServiceController sc = new ServiceController(services[i].ServiceName, remotemachinename); if(sc.Status = = ServiceControllerStatus.Stopped) { } } Looking for the response. What could be the problem Balaji { would be that the
waits until the service is running or a timeout occurs. public static void StartService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { / / . . . } } Stop service The following method tries to stop the specified service and waits until the service is stopped or a timeout occurs. public static void StopService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } catch { / / . . . } } Restart service This method combinates both previous methods. It tries to stop running). The specified timeout is used for both operations together. public static void RestartService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus
Registry.LocalMachine.OpenSubKey (1) SmoApplication.EnumAvailableSqlServers (1) SQL Server (1) SqlDataSourceEnumerator.Instance (1) SqlDataSourceEnumerator (1) ServiceControllerStatus (1) Hallo Klaus, Bei lokale(n) Instanz(en) entspricht der ServerName dem Rechnernamen, filtern kannst hoffe ich, beide Fliegen mit einer Klappe zu schlagen (CLI .Net): System::String ^CheckService() { String ^servicename = 3D String::Empty; String ^machinename = 3D "."; array<ServiceController^> ^services = 3D nullptr; try{ services = 3D ServiceController return String::Empty; } for(Int32 i = 3D 0; i < services-> Length; i++){ if(services[i]-> ServiceName-> ToString()-> Contains("MSSQL$")){ if (services[i]-> Status ! = 3D ServiceControllerStatus::Running && services[i]-> Status ! = 3D ServiceControllerStatus::StartPending && services[i]-> Status ! = 3D ServiceControllerStatus::ContinuePending) { try{ array<String^> ^args = 3D gcnew array<String^> (4); args[0] = 3D "MSSQLSERVER"; args 3D Convert::ToString(TimeSpan(0, 0, 0, 5)); services[i]-> Start(args); services[i]-> WaitForStatus(ServiceControllerStatus::Running, TimeSpan(0, 0, 0, 10)); servicename = 3D services[i]-> ServiceName-> ToString(); } catch(Exception ^e){ services[i]-> Continue(); services[i]-> WaitForStatus(ServiceControllerStatus
this .AfterInstall + = new InstallEventHandler(ServiceInstaller_AfterInstall); } void ServiceInstaller_AfterInstall( object sender, InstallEventArgs e) { ServiceController sc = new ServiceController( "ServiceName" ); sc.Start(); } you can do in this way also Add the following class to your waits until the service is running or a timeout occurs. public static void StartService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { / / . . . } } Stop service The following method tries to stop the specified service and waits until the service is stopped or a timeout occurs. public static void StopService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } catch { / / . . . } } Restart service This method combinates both previous methods. It tries to stop running). The specified timeout is used for both operations together. public static void RestartService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan
until the service is running or a timeout occurs. [C#] public static void StartService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController (serviceName); try { TimeSpan timeout = TimeSpan .FromMilliseconds(timeoutMilliseconds); service. Start (); service. WaitForStatus ( ServiceControllerStatus . Running , timeout); } catch { / / . . . } } Stop service The following method tries to stop the specified service and until the service is stopped or a timeout occurs. [C#] public static void StopService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController (serviceName); try { TimeSpan timeout = TimeSpan .FromMilliseconds(timeoutMilliseconds); service. Stop (); service. WaitForStatus ( ServiceControllerStatus . Stopped , timeout); } catch { / / . . . } } Restart service This method combinates both previous methods. It tries to stop The specified timeout is used for both operations together. [C#] public static void RestartService( string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController (serviceName); try { int millisec1 = Environment .TickCount; TimeSpan timeout = TimeSpan .FromMilliseconds(timeoutMilliseconds); service. Stop (); service.WaitForStatus( ServiceControllerStatus
until the service is running or a timeout occurs. [C#] public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { / / . . . } } Stop service The following method tries to stop the specified service and until the service is stopped or a timeout occurs. [C#] public static void StopService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } catch { / / . . . } } Restart service This method combinates both previous methods. It tries to stop The specified timeout is used for both operations together. [C#] public static void RestartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus
com C# - German Discussions System.Collections.IDictionary (1) Visual Studio 2008 (1) Windows Installer (1) ServiceControllerStatus (1) File.AppendAllText (1) ServiceStartAndStopInstaller (1) Hallo Klaus, Nein, die CAs werden "bei" der Installation Das Stoppen "kann" ggf. so implementiert werden: / / using System.ServiceProcess; ServiceController sc = new ServiceController(); sc.ServiceName = "DeinServiceName"; Console.WriteLine( sc.Status.ToString()); if (sc.Status = = ServiceControllerStatus.Stopped) { Console.WriteLine("Starte den " + sc.DisplayName + " . . ."); try { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); Console.WriteLine("Der Status des Dienstes: " + sc.DisplayName + " ist jetzt: {0}.", sc.Status.ToString Stopping of the Service / / / < / summary> public class ServiceStartAndStopInstaller : Installer { ServiceController GetServiceController() { ServiceController controller = new ServiceController("SERVICENAME"); return controller; } void StartService() { using(ServiceController controller = GetServiceController()) { if (controller.Status = = ServiceControllerStatus.Stopped) controller.Start(); } } void StopService() { using(ServiceController controller = GetServiceController()) { if(controller.Status = = ServiceControllerStatus.Running) controller.Stop(); } } public override void Install(System.Collections.IDictionary stateSaver) { StartService(); base.Install(stateSaver