Start & Stop Services through WinForms

Windows Services are long running programs (analogous to UNIX daemons)
that perform task without user interaction. Services are controlled by a
system service know as the Service Control Manager (SCM). This sample uses the
ServiceController class from the System.ServiceProcess namespace.


// Used to access an instance of the selected service.
private ServiceController msvc;
// Used to control UI updates.

private bool fUpdatingUI ;

private void cmdStart_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 cmdStop_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);
}
}


Update the command buttons for the selected service.

private void UpdateUIForSelectedService()
{
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]) );
//msvc = ((ServiceController) (mcolSvcs[ItemIndex]) );

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


Load Servies ON Page Load

private void frmMain_Load(object sender, System.EventArgs e)
{
EnumServices();
}

private void EnumServices()
{
// Get the list of available services and
// load the list view control with the information
try
{
fUpdatingUI = true;
this.sbInfo.Text = "Loading Service List, pleasse wait";
this.sbInfo.Refresh();
this.lvServices.Items.Clear();
if (!(mcolSvcs == null))
{
mcolSvcs = new Hashtable();
}

//svcs = new ServiceController[];
ServiceController[] svcs = ServiceController.GetServices();
foreach(ServiceController svc in svcs)
{
ViewItem = this.lvServices.Items.Add(svc.DisplayName);
ViewItem.SubItems.Add(svc.Status.ToString());
ViewItem.SubItems.Add(svc.ServiceType.ToString());

//mcolSvcs.Add(svc, svc.DisplayName); mcolSvcs.Add(svc.DisplayName,svc);
//Next svc++;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.sbInfo.Text = "Ready";
fUpdatingUI = false;
}
}


Comments

Author: Sujit Sahu21 Apr 2009 Member Level: Bronze   Points : 0

Best If You Post Executable File with Source File



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: