1

While attempting to start my Windows Service the following error suddenly appears when the status bar is almost at 50 % :

Error 1053: The service did not respond to the start or control request in timely fashion

When I check the Event Viewer I see the following errors:

Application: EnvMonService.exe

Framework Version: v4.0.30319

Description: The process was terminated due to an unhandled exception

Exception Info: System.NullReferenceException

Stack: at EnvMonService.EnvMonService..ctor() at EnvMonService.Program.Main()

I cannot see where the error comes from - here is my code:

Main()

static void Main()
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
         new EnvMonService() 
    };
    ServiceBase.Run(ServicesToRun);
}

Constructor

public EnvMonService()
{
    InitializeComponent();
}

OnStart and OnStop

protected override void OnStart(string[] args)
{
    // Start a task thread polling the databse for changes and updates the ASCII file
    mainTask = new Task(PollDatabase, cts.Token, TaskCreationOptions.LongRunning);
    mainTask.Start();
}


protected override void OnStop()
{
     // Cancel Task and wait for it to be disposed
     cts.Cancel();
     mainTask.Wait();
}

My Variables

// Connection to database
private string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

// Newest timestamp
private Int32 latestDate;

// Log file
public LogFileWriter logFileWriter;

// Define datastructure POCO class
EnvDataModel model = new EnvDataModel();

// Instantiate a concellation token for a task
private CancellationTokenSource cts = new CancellationTokenSource();

// Instantiate a task
private Task mainTask = null;

// Define a waiting interval between each database polling
private TimeSpan WaitAfterSuccessInterval = new TimeSpan(0, 1, 0);

// Define a waiting interval if any errors happens
private TimeSpan WaitAfterErrorInterval = new TimeSpan(0, 2, 0);

Code running in the Task

    private void PollDatabase()
    {
     // New token
     CancellationToken cancellation = cts.Token;

     // Fencepost problem
     // Setup default interval
     TimeSpan interval = TimeSpan.Zero;

     // Create a new logfile instance
     logFileWriter = new LogFileWriter(@"C:\LogFile.txt");

     // Poll database until service stops
     while (!cancellation.WaitHandle.WaitOne(interval))
     {
          try
          {

              // Poll database here and assign reponse to EnvDataModel

              // Update status in logfile
              logFileWriter.WriteToLogFile("Database Last Polled: " + DateTime.Now + Environment.NewLine);

              // Call ASCII File Parser
              ParseDataToASCIIFileAsync();

              // Occasionally check the cancelation state
              if (cancellation.IsCancellationRequested)
              {
                  break;
              }
              interval = WaitAfterSuccessInterval;
           }
           catch (Exception caught)
           {
               // Log exception
               logFileWriter.WriteToLogFile(caught.Message);

               // Wait a few minutes before trying again
               interval = WaitAfterErrorInterval;
           }
      }
}
VBMan
  • 145
  • 3
  • 9
  • Try deploying the service with `.pdb` files - this will give you nicer stacktraces, even with line numbers. – Anton Gogolev Aug 26 '15 at 09:16
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – TheLethalCoder Aug 26 '15 at 09:18
  • @AntonGogolev How do I do that? Right now my code is placed on network drive and I have just copied the `.exe` file to my local drive. So do I just have copy the `.pdb` file to the same location? – VBMan Aug 26 '15 at 09:20
  • 1
    @VBMan Pretty much. To be on the safe side, delete everything from your network folder, clean up your solution and then copy everything from `bin\Debug` folder. – Anton Gogolev Aug 26 '15 at 09:21
  • You could use Visual Studios to debug the Windows Service by attaching to the process: https://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx – TheTerribleProgrammer Aug 26 '15 at 09:22
  • @AntonGogolev That did the trick - the service is now running without errors! – VBMan Aug 26 '15 at 09:28

1 Answers1

2

I solved my issue by the following steps:

  • In Visual Studio, right click on your Solution and click Clean Solution
  • Right click, again, on your solution and click Build Solution
  • [Not Mandatory] Copy all the files from bin/debug folder to the directory you want to run the service from. The service needs to be on your local drive.
  • Install service using Visual Studio Developer Command Prompt and installutil
VBMan
  • 145
  • 3
  • 9