Thomas Thomas - 3 months ago 36
C# Question

How to pass some parameter to windows service when run programmatically

i like to know that when we start & stop win service programmatically from another apps then how could i pass some parameter to windows service programmatically....is it possible.

here is my sample code which i use to start service programmatically.

private const int RestartTimeout = 10000;

private readonly ServiceController service;

public Control(string serviceName, string computerName)
{
service = new ServiceController(serviceName, computerName);
}

public Control(string serviceName)
{
service = new ServiceController(serviceName);
}

public bool StartService()
{
try
{
service.Refresh();

if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
return true;
}

MessageBox.Show(string.Format("{0} --> already started", service.DisplayName));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), @"Error Starting Service");
}

return false;
}

jrv jrv
Answer

The ServiceController.Start() method has an overload that takes an array of strings for parameters which can be implemented further. See http://msdn.microsoft.com/en-us/library/9c38b683.aspx