Start, Stop, Pause, Resume a service

Bubbly
Posted by Bubbly under C# category on | Views : 2597
Read this article to load a service : http://www.dotnetfunda.com/codes/code717-load-list-of-services.aspx

private void cmdStartService_Click(object sender, System.EventArgs e)
{
try
{
msvc.Start();
fUpdatingUI = true;
UpdateUIForSelectedService();
fUpdatingUI = false;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cmdResumeService_Click(object sender, System.EventArgs e)
{
try
{
msvc.Continue();
fUpdatingUI = true;
UpdateUIForSelectedService();
fUpdatingUI = false;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cmdPauseService_Click(object sender, System.EventArgs e)
{
try
{
msvc.Pause();
fUpdatingUI = true;
UpdateUIForSelectedService();
fUpdatingUI = false;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cmdStopService_Click(object sender, System.EventArgs e)
{
try
{
msvc.Stop();
fUpdatingUI = true;
UpdateUIForSelectedService();
fUpdatingUI = false;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateUIForSelectedService()
{
// Update the command buttons for the selected service.
string strName;
int ItemIndex;

try
{
if (this.lvServices.SelectedItems.Count == 1)
{
strName = this.lvServices.SelectedItems[0].SubItems[0].Text;
ItemIndex = this.lvServices.FocusedItem.Index;
msvc = ((ServiceController) (mcolSvcs[strName]) );
// if it's stopped, we should be able to start it
this.cmdStart.Enabled = (msvc.Status == ServiceControllerStatus.Stopped);
// Check if we're allowed to try and stop it and make sure it's not already stopped.
this.cmdStop.Enabled = (msvc.CanStop && (!(msvc.Status == ServiceControllerStatus.Stopped)));
// Check if we're allowed to pause it and see if it is not paused already.
this.cmdPause.Enabled = (msvc.CanPauseAndContinue && (!(msvc.Status == ServiceControllerStatus.Paused)));
// if it's paused, we must be able to resume it.
this.cmdResume.Enabled = (msvc.Status == ServiceControllerStatus.Paused);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Comments or Responses

Login to post response