Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not showing page correctly 3 29
ASP.NET 5 Templates 2 65
Error in JQuery 5 35
how can i get the executable application's path in a specific form? ('\\' instead of '\ ') 5 25
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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 is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

10 Experts available now in Live!

Get 1:1 Help Now