Mar 2, 2010

Start, Stop, Pause, Resume a service

Read this article to load a service : http://angeldeeps.blogspot.com/2010/03/load-list-of-services.html

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);
}
}

No comments:

Post a Comment