hello Mahesh how r u ?
i was trying your code which is about window services that how to start and stop by using c#, but there is a problem or exception is occure that " MyServiceName can not open on computer: "
actually first i created window service and installed that then i was start that service through your code but that exception is occure by all time..
Kindly send me the solution of that please..
Reply Soon
thanks..
Loading
Faisal AnsariPosted Jun 20, 2011, 4:50 AM
" Can not open FaisalService service on Computer"
Posted Jun 20, 2011, 4:49 AM
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add(new DataColumn("Name"));
dt.Columns.Add(new DataColumn("DisplayName"));
dt.Columns.Add(new DataColumn("Status"));
GetAllServices();
}
private void GetAllServices()
{
dt.Rows.Clear();
ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", string.Format("SELECT * FROM Win32_Service "));
DataRow dr;
foreach (ManagementObject result in mos.Get())
{
ServiceController sc = new ServiceController(result["Name"].ToString());
dr = dt.NewRow();
dr["Name"] = sc.ServiceName;
dr["DisplayName"] = sc.DisplayName;
dr["Status"] = sc.Status;
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
}
public static void StartService(string serviceName, int timeoutMilliseconds)
{
//start the service
ServiceController service = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
public static void StopService(string serviceName, int timeoutMilliseconds)
{
//stop the service
ServiceController service = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
//stop the service and then start the service
ServiceController service = new ServiceController(serviceName);
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
StartService(Convert.ToString(dr.Cells[0].Value), 10000);
GetAllServices();
}
private void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
StopService(Convert.ToString(dr.Cells[0].Value), 10000);
GetAllServices();
}
private void button3_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
RestartService(Convert.ToString(dr.Cells[0].Value), 10000);
GetAllServices();
}
Posted Jun 20, 2011, 4:41 AM
Posted Jun 20, 2011, 4:39 AM
Have you analysed the Event Viewer logs ?
To enable debugging in windows service application, Add , Debugger.Launch() in the start method(). Once it hits the start method, debugger window will shown to attach the code base.
Faisal AnsariPosted Jun 20, 2011, 4:39 AM
Reply Soon please...