Windows Service Won't Start

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
tinman1412Asked:
Who is Participating?
 
checooCommented:
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
 
jjacksnCommented:
what do you mean by timeout?  I am unaware that the service manager will timeout services.  
0
 
tinman1412Author Commented:
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
 
checooCommented:
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
 
tinman1412Author Commented:
New Thread?. What do you mean by this?. Can you provide code samples?

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.