• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

Service, Stuck in starting state. Loop Code Probably in wrong place....

My Code is checking for certain exceptions every minute, however the service is always locked in a starting state... Any ideas?

Start Code SNIPPET

            protected override void OnStart(string[] args)
                  StopService = true;

                        string buildItem="";
                        StreamReader FileIn = new StreamReader(@"D:\\PrymakBuilds\\Builds\\buildItems");

                              while (buildItem != null)
                                    buildItem = FileIn.ReadLine();
                                    if (buildItem != null)
                              FileIn = null;


1 Solution
Not really sure what the problem is but from this code it does not seem like you have any means of exiting the loop. Maybe you need some statement that will change the criteria in the while to false ?

Is D:\\PrymakBuilds\\Builds\\buildItems is a file, the error probably could be that you are getting exception in FileIn.ReadLine();

The service will be in the "starting" state until the OnStart routine exits. Therefore, the actual _work_ that the service will do (i.e., periodic checking of x,y,z) must be implemented in an asychronous manner (in your case, probably using a Timer control or by using the System.Threading.Timer mechanism).

A pretty good article on performing tasks at set intervals using the Timer control inside a windows service: http://www.15seconds.com/issue/021007.htm
The first portion of the article ("Step One") should get you well on your way.
Rowdyone52Author Commented:

The article was very helpful however I cannot figure out where to put the function pasted below within my service project.  Any ideas?

public scheduler()

                  string servicepollinterval = System.Configuration.ConfigurationSettings.AppSettings

                  double interval=10000;
                        interval = Convert.ToDouble(servicepollinterval);
                  catch(Exception) {}
                  timer = new System.Timers.Timer(interval);
                  timer.Elapsed += new ElapsedEventHandler( this.ServiceTimer_Tick );

You want all of this code to be in the constructor for your class.
Additionally, you'll want to actually declare the timer as a private member of the class, so outside of any other function (but within the braces for your class), you'll want to declare something like:

    private System.Timers.Timer timer;

Then in the OnStart method you can simply call:

      timer.Enabled = true;

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now