Solved

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

Posted on 2004-09-21
5
454 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now