Friday, April 8, 2011

How to programatically pass arguments to OnStart method of a windows service ?

Hi All, How do i programatically pass in arguments to OnStart method of a Service, which I further need to propagate to the Elapsed event of the Timer inside the service.

  • Amit
From stackoverflow
  • You could consider having the OnStart method read the arguments from a configuration file and programatically update that, using either a separate application.

  • At the simplest level: when you call ServiceBase.Run, you get to give it the service instances to execute. Simply declare this as a public property on your service, and assign prior to calling Run:

            Service1 myService = new Service1();
            myService.SomeProp = 1;
            ServiceBase.Run(myService);
    

    Then read SomeProp in your service:

        public int SomeProp { get; set;}
        protected override void OnStart(string[] args)
        {
            int prop = SomeProp;
        }
    

    You can also use the service args, but that is from the external caller (service registration) - not programatically (per the question).

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.