?
Solved

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

Posted on 2004-09-21
5
Medium Priority
?
605 Views
Last Modified: 2008-02-01
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;

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

                        try
                        {
                        
                              
                              while (buildItem != null)
                              {
                                    buildItem = FileIn.ReadLine();
            
                                    if (buildItem != null)
                                          CheckForUpdate(buildItem);
                              }
                              FileIn.Close();
                              FileIn = null;

                              CheckForDocs();
                                      
                              System.Threading.Thread.Sleep(60000);
                        
                        }
                        catch
                        {
                  
                        }

                  }
            }
0
Comment
Question by:Rowdyone52
5 Comments
 

Expert Comment

by:flalar
ID: 12119527
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 ?


Cheers
Lars
0
 
LVL 18

Expert Comment

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

0
 
LVL 1

Accepted Solution

by:
micaiah earned 2000 total points
ID: 12120093
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.
0
 

Author Comment

by:Rowdyone52
ID: 12122742
Micaiah,

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
                        ["servicepollinterval"];

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

            }
0
 
LVL 1

Expert Comment

by:micaiah
ID: 12130540
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;
      timer.Start();

hth,
m
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question