none
C# Windows Service running twice RRS feed

  • Question

  • i have a windows service that looks something like this:

    public sealed class MyService : ServiceBase
    {
        public MyService()
        {
            InitializeComponent();
            Log("constructor called");
        }

        public override void OnStart(string[] args)
        {
            Log("onstart called");
        }
    }

    Once i start my service, my log looks like this:
    constructor called
    constructor called
    onstart called
    onstart called


    What i am noticing is that I am having two services running at the same time. why is that? can i fix this?   
    Thursday, June 7, 2007 10:36 PM

Answers

  • Check your Static Main method in the project to ensure that only one instance is added to the ServiceBase.Run() call. The Run call accepts an array of service instances and the default one created by the project wizard creates one instance (Service1) and comments out a second instance as an example how to start up more than one service at a time. You may have inadvertently added two instances to the array. Your static Main method should look something like:

    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] { new MyService() };
    ServiceBase.Run( ServicesToRun );


    Cheers,

    Friday, June 8, 2007 12:13 AM

All replies

  • Check your Static Main method in the project to ensure that only one instance is added to the ServiceBase.Run() call. The Run call accepts an array of service instances and the default one created by the project wizard creates one instance (Service1) and comments out a second instance as an example how to start up more than one service at a time. You may have inadvertently added two instances to the array. Your static Main method should look something like:

    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] { new MyService() };
    ServiceBase.Run( ServicesToRun );


    Cheers,

    Friday, June 8, 2007 12:13 AM
  • Hi,

    Here is one article Simple Windows Service Sample with detail steps.

    Please refer to it!

    Monday, June 11, 2007 6:57 AM
  • Hey there, did you get a solution for this?  Was it a problem with the trace, or with the service?

    ta

    Monday, June 18, 2007 10:03 PM
  • Ah, there must be a Listener running by default in VS?  If your symptom is jsut the duplicate messages, not duplicate services as such, then don't add a new Listener;

                //Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
                Trace.AutoFlush = true;
                Trace.Indent();
                Trace.WriteLine("There are now " + Trace.Listeners.Count + " trace Listeners");
    Monday, June 18, 2007 10:16 PM
  • Same thing happens to me. I have a FTP service that is supposed to run with an authenticated user.

    When I start the service, it runs as "SYSTEM" for the first instance and the authenticated user as the second instance.

    I wrote code to return from OnStart when it encounters "SYSTEM" as the user.

    Can this be because I build it as "all-cpus" and both x32 and x64 are being run simultaneously?

    Friday, August 23, 2019 1:28 PM