Wrox Programmer Forums
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2006, 08:23 AM
Authorized User
 
Join Date: Sep 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Auto Start Windows Service

Hai all, I have written windows service application.In StartType i selected automatic.But After i install the service it was not starting automatically.i should start it manually.Please Let me know what other settings should i make.

Thanks In Advance

__________________
Check It out
 
Old November 28th, 2006, 07:02 AM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

The StartType property specifies the behavior of a service at system startup. It doesn't address the issue of starting a service after you install a Windows Service using the Setup Project.

There are two ways you can address this issue: either instruct your user to restart the computer after installation or write an installer class for starting your service after it gets installed.

You will need to create a library with a class that derives from the System.Configuration.Install.Installer class. Decorate your class with [RunInstaller(true)]. Now override the OnCommitted method and write the following code inside the method:

try
{
  string serviceName = "<your_service_name>";
  ServiceController serviceController =
    new ServiceController(serviceName);
  serviceController.Start();
}
catch
{
 //Handle exception.
}

Compile the project and add its output to the Commit custom action of your Setup project that installs your service. This will start your service as soon as it's installed. Setting the StartType property to Automatic will ensure your service starts automatically each time the computer starts.

Hopefully this helps.

Cheers.

ejan
The Following User Says Thank You to ejan For This Useful Post:
 
Old December 26th, 2006, 09:42 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default

i was also looking for the same answer!


thanks......
 
Old February 3rd, 2007, 02:07 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ejan, thank you for the great tip. but need a bit more help.

when I uninstall the program I get a popup asking me do I want to stop the service I uninstalling. I try many ways, example

I created the override OnBeforeUninstall() here my code

        protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);
            try
            {
                ServiceController service = new ServiceController("SathaiDynIP");
                service.Stop();

                service.WaitForStatus(ServiceControllerStatus.Stop ped);
            }
            catch
            {
                //Handle exception.
            }
        }

But it doesn't stop the sevices before uninstalling. Can you help?
Thank in advance.

JamesNW
 
Old April 12th, 2008, 08:05 AM
Registered User
 
Join Date: Jul 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried as your code in vb.net but it is not start once when I finish install. Please help.

Public Overrides Sub Commit(ByVal savedState As IDictionary)
            Dim serviceName As String = "MyService"
            Dim serviceController As New System.ServiceProcess.ServiceController(serviceNam e)
            serviceController.Start()
    End Sub



Quote:
quote:Originally posted by ejan
 The StartType property specifies the behavior of a service at system startup. It doesn't address the issue of starting a service after you install a Windows Service using the Setup Project.

There are two ways you can address this issue: either instruct your user to restart the computer after installation or write an installer class for starting your service after it gets installed.

You will need to create a library with a class that derives from the System.Configuration.Install.Installer class. Decorate your class with [RunInstaller(true)]. Now override the OnCommitted method and write the following code inside the method:

try
{
  string serviceName = "<your_service_name>";
  ServiceController serviceController =
    new ServiceController(serviceName);
  serviceController.Start();
}
catch
{
 //Handle exception.
}

Compile the project and add its output to the Commit custom action of your Setup project that installs your service. This will start your service as soon as it's installed. Setting the StartType property to Automatic will ensure your service starts automatically each time the computer starts.

Hopefully this helps.

Cheers.

ejan
 
Old April 18th, 2008, 11:43 AM
Registered User
 
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's what you should enter:

Code:
<Security.Permissions.SecurityPermission(Security.Permissions.SecurityAction.Demand)> _
Public Overrides Sub Commit(ByVal savedState As System.Collections.IDictionary)

        MyBase.Commit(savedState)

        'Put the code to start your service here.
        Dim serviceName As String = "MyService"
        Dim serviceController As New System.ServiceProcess.ServiceController(serviceName)
        serviceController.Start()

    End Sub


Quote:
quote:Originally posted by zawmn83
 I tried as your code in vb.net but it is not start once when I finish install. Please help.

Public Overrides Sub Commit(ByVal savedState As IDictionary)
            Dim serviceName As String = "MyService"
            Dim serviceController As New System.ServiceProcess.ServiceController(serviceNam e)
            serviceController.Start()
    End Sub



Quote:
quote:Originally posted by ejan
 The StartType property specifies the behavior of a service at system startup. It doesn't address the issue of starting a service after you install a Windows Service using the Setup Project.

There are two ways you can address this issue: either instruct your user to restart the computer after installation or write an installer class for starting your service after it gets installed.

You will need to create a library with a class that derives from the System.Configuration.Install.Installer class. Decorate your class with [RunInstaller(true)]. Now override the OnCommitted method and write the following code inside the method:

try
{
string serviceName = "<your_service_name>";
ServiceController serviceController =
    new ServiceController(serviceName);
serviceController.Start();
}
catch
{
//Handle exception.
}

Compile the project and add its output to the Commit custom action of your Setup project that installs your service. This will start your service as soon as it's installed. Setting the StartType property to Automatic will ensure your service starts automatically each time the computer starts.

Hopefully this helps.

Cheers.

ejan





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Auto Start Windows Service virendar23 C# 2005 3 September 15th, 2011 03:16 PM
How to auto. Start App As User on Server? BrianWren Pro VB 6 12 April 29th, 2008 05:03 PM
Windows Service auto login theBadKid C# 2005 1 November 30th, 2007 05:04 AM
SQL Server "Service Manager" Auto Start Problem. khalidumer SQL Server 2000 0 May 18th, 2004 09:21 AM
auto start smukher Pro VB 6 2 October 15th, 2003 01:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.