Solved

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

Posted on 2004-09-21
5
489 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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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