Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I created simple windows service that beeps after a interval of two seconds. When I try to Install the service using the application installer that is the exe file, it gives an error that "Cannot start service form the command line or a debugger. A windows service must first be installed (using installutil.exe) and then started with the ServerExplorer, Windows Services Administrative tool or the NET START command." but beep after 2 seconds the beep is generated while the error message is displayed.
I tried installing the service using the installutil form .NET command prompt the service is successfully installed but the sound of beep is absent. Here is the code I used. I do not have any experience in service programming so there may be an error in understating the life cycle of service. I have experience in ASP.NET development so little hint can help me sort out things.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
            timer1.Start();
        }
        protected override void OnStart(string[] args)
        {
            timer1.Start();
        }
        protected override void OnStop()
        {
        }
        //private void InitializeComponent()
        //{
        //    timer1.Start();
        //}
        private void timer1_Tick(object sender, EventArgs e)
        {
             Console.Beep();
        }
        static void main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            // More than one user Service may run within the same process. To add
            // another service to this process, change the following line to
            // create a second service object. For example,
            //
            //   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
            //
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
    }
}
Posted
Updated 27-Jun-11 6:34am
v2

Have a look at the link How to create a setup project for a Windows Service application in Visual C# [^] it is explained with detail and sample.
 
Share this answer
 
Comments
Uday P.Singh 27-Jun-11 13:59pm    
good answer my 5!
You need to have an installer project in your service solution, and you need to modify that project according to the way youtr service is installed. Google is your friend.

I searched for "C# windows service installer project codeproject" on google and got 740,000 results.
 
Share this answer
 
v2
installutil YourapplicationName\bin\debug\YourApplicaionName.exe....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900