Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 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