I am developing a self-installable Windows Service. I have a ServiceManager class that handles the installation / uninstallation, and seems to work correctly.
However, when I try to run the start the service I get, immediately, a MessageBox with the following text:
Could not start service on local system. Error 1053: The service didn't respond to the start or control request in a timely fashion.
My Service code:
public partial class MyService : ServiceBase
{
public MyService()
{
using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
{
tw.WriteLine("Test1");
tw.Close();
}
InitializeComponent();
}
protected override void OnStart(string[] args)
{
using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
{
tw.WriteLine("Test2");
tw.Close();
}
Thread t = new Thread(new ThreadStart(InitService));
t.Start();
}
protected override void OnStop()
{
}
static void InitService()
{
using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
{
tw.WriteLine("Test4");
tw.Close();
}
while (true)
{
Thread.Sleep(100);
}
}
The designer code:
namespace MyService
{
partial class MyService
{
/// <summary>
/// Variable del diseñador requerida.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Limpiar los recursos que se estén utilizando.
/// </summary>
/// <param name="disposing">true si los recursos administrados se deben eliminar; false en caso contrario, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Código generado por el Diseñador de componentes
/// <summary>
/// Método necesario para admitir el Diseñador. No se puede modificar
/// el contenido del método con el editor de código.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "MyService";
}
#endregion
}
}
My Main:
public static void Main(string[] args)
{
try
{
bool debug = false;
bool mustRun = false;
string errMsg = "";
mustRun = true;
using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
{
tw.WriteLine("Test3");
tw.Close();
}
ServiceManager manager = new ServiceManager();
mustRun = !manager.ParseArgs(args, ref debug, ref errMsg);
MyService svc = new MyService();
//
// if (debug)
// {
// svc.DebugStart();
// }
// else
if (mustRun)
{
System.ServiceProcess.ServiceBase[] ServicesToRun = null;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { svc };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
else
{
}
}
catch (Exception ex)
{
using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
{
tw.WriteLine(ex.Message);
tw.Close();
}
}
}
File C:\Test.txt isn't written to, except when installing or uninstalling the service (With Test3 string). I'm using Windows XP so there shouldn't be permission problems.
I have tried installing the service with the System and NetworkService account, and the same problem arises.
With the administrator account it gives me another error, related to the account (I guess it's because I don't have a password).
Any idea about what happens?
Update: Answering David's question, this is what the event log says (hand translation, the english message could be different):
The service cannot start. The service process cannot connect with the service controller.
Update 2: Looking more carefully to the Applicatin log, the previous error was due to a mismatch between the Service installation name and the ServiceName property in the designer. It was already fixed; now there aren't new errors in the event log but the problem persists.