1

I have the windows service program which runs and sometime throw exception about system null reference. After my investigation, this is due to MySQL connection cannot establish due to MySql instance not up yet when startup computer. How to solve this problems???

thanks in advance

3 Answers 3

2

You can set in Windows the dependencies of each service.

You can go into Control Panel -> Administrative Tools -> Services (or run 'services.msc' from the command line). By double clicking any of the services and going to the Dependencies tab you can see what each service relies on.

Your service relies on the MySql service, so MySql should be in the dependencies list for it.

You can add items to the dependencies, here is a description of how to do this:

https://serverfault.com/questions/24821/how-to-add-dependency-on-a-windows-service-after-the-service-is-installed

1

This article can give you a hint on how code service dependencies:

http://bloggingabout.net/blogs/jschreuder/archive/2006/12/07/How-to_3A00_-Code-Service-Dependencies.aspx

2
  • +1 nice linked article, that one's going straight to the pool room
    – Dan F
    Jul 16, 2011 at 4:21
  • i will refer this link it works,but when i will restart the computer 2 times then it works & one blank message box shown by computer in MSG box two buttons display yes or no if press yes it will restart the computer & press no it will not doing anything why this button shown by computer
    – user847455
    Jul 16, 2011 at 7:48
1

Try the following code if you want to do it by yourself without dependencies

//Check if service is running
ServiceController mysqlServiceController = new ServiceController();
mysqlServiceController.ServiceName = "MySql";
var timeout = 3000;

//Check if the service is started
if (mysqlServiceController.Status == System.ServiceProcess.ServiceControllerStatus.Stopped
                || mysqlServiceController.Status == System.ServiceProcess.ServiceControllerStatus.Paused)
{
      mysqlServiceController.Start();

      try
      {
           //Wait till the service runs mysql
           ServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, new TimeSpan(0, timeout, 0));
      }
      catch (System.ServiceProcess.TimeoutException)
      {
          //MessageBox.Show(string.Format("Starting the service \"{0}\" has reached to a timeout of ({1}) minutes, please check the service.", mysqlServiceController.ServiceName, timeout));
      }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.