Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Windows Service Won't Start

Posted on 2005-05-09
5
Medium Priority
?
280 Views
Last Modified: 2012-05-05
Hello,

I have a windows service that works fine. All it does it write something to a text file. I use "InstallUtil" to register the service and runs it manually
in the Service Controller by right-clicking on it and select "Start". The problem I have is that I have a service that takes several minutes b/c
it's retrieving a large amount of data from the database. It takes around 2 minutes when testing from my web page. When I manually start the service,
it would timeout around 90 seconds. Is there a registry setting or something where I can increase the time for a service to start. Please advise

Thanks
0
Comment
Question by:tinman1412
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:jjacksn
ID: 13965175
what do you mean by timeout?  I am unaware that the service manager will timeout services.  
0
 

Author Comment

by:tinman1412
ID: 13965192
Yes it does. Just write something that will take over 2 minutes and make it a window service. You will notice that your service will try to start but it doesn't do anything, and you get a weird prompt. I don't remember what that prompt is at the moment
0
 
LVL 9

Expert Comment

by:checoo
ID: 13965307
I dont remember whether its 30 or 90 seconds but there is a time limit within which the onStart method has to finish its execution, otherwise the system thinks that the service is malfunctioning and you will get an error message.

Because of this its not advisable to have any long running task in the onStart method. But if this cant be avoided then as in your case you should spwan of a new thread in the onstart method and have that thread to the file writting job
0
 

Author Comment

by:tinman1412
ID: 13965360
New Thread?. What do you mean by this?. Can you provide code samples?

Thanks
0
 
LVL 9

Accepted Solution

by:
checoo earned 500 total points
ID: 13965404
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace WindowsService1
{
      public class Service1 : System.ServiceProcess.ServiceBase
      {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Service1()
            {
                  // This call is required by the Windows.Forms Component Designer.
                  InitializeComponent();

                  // TODO: Add any initialization after the InitComponent call
            }

            // The main entry point for the process
            static void Main()
            {
                  System.ServiceProcess.ServiceBase[] ServicesToRun;
      
                  // More than one user Service may run within the same process. To add
                  // another service to this process, change the following line to
                  // create a second service object. For example,
                  //
                  //   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
                  //
                  ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

                  System.ServiceProcess.ServiceBase.Run(ServicesToRun);
            }

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  components = new System.ComponentModel.Container();
                  this.ServiceName = "Service1";
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            /// <summary>
            /// Set things in motion so your service can do its work.
            /// </summary>
            ///

            private Thread myThread;

            protected override void OnStart(string[] args)
            {
                  // TODO: Add code here to start your service.

                  myThread = new Thread(new ThreadStart(writeToMyFile));
                  myThread.Start();
            }
 
            private void writeToMyFile()
            {
                  //write code here to get values from database and write to a file
            }
            

            /// <summary>
            /// Stop this service.
            /// </summary>
            protected override void OnStop()
            {
                  // TODO: Add code here to perform any tear-down necessary to stop your service.
                  myThread.Abort();
            }
      }
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

564 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