Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#

Using .NET 2.0 to Create a Windows Service

Rate me:
Please Sign up or sign in to vote.
1.77/5 (10 votes)
30 Mar 2008CPOL4 min read 37.4K   268   20  
This article describes Windows Services and their creation.
#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ServiceProcess;

#endregion

namespace ServiceControl
{
	partial class ServiceControlForm : Form
	{
		private System.ServiceProcess.ServiceController[] services;

		public ServiceControlForm()
		{
			InitializeComponent();
			RefreshServiceList();
		}

		private void RefreshServiceList()
		{
			services = ServiceController.GetServices();
			listBoxServices.DisplayMember = "DisplayName";
			listBoxServices.DataSource = services;

		}

		protected string GetServiceTypeName(ServiceType type)
		{
			string serviceType = "";
			if ((type & ServiceType.InteractiveProcess) != 0)
			{
				serviceType = "Interactive ";
				type -= ServiceType.InteractiveProcess;
			}
			switch (type)
			{
				case ServiceType.Adapter:
					serviceType += "Adapter";
					break;
				case ServiceType.FileSystemDriver:
				case ServiceType.KernelDriver:
				case ServiceType.RecognizerDriver:
					serviceType += "Driver";
					break;
				case ServiceType.Win32OwnProcess:
					serviceType += "Win32 Service Process";
					break;
				case ServiceType.Win32ShareProcess:
					serviceType += "Win32 Shared Process";
					break;
				default:
					serviceType += "unknown type " + type.ToString();
					break;
			}
			return serviceType;
		}

		protected void SetServiceStatus(ServiceController controller)
		{
			buttonStart.Enabled = true;
			buttonStop.Enabled = true;
			buttonPause.Enabled = true;
			buttonContinue.Enabled = true;
			if (!controller.CanPauseAndContinue)
			{
				buttonPause.Enabled = false;
				buttonContinue.Enabled = false;
			}
			if (!controller.CanStop)
			{
				buttonStop.Enabled = false;
			}
			ServiceControllerStatus status = controller.Status;
			switch (status)
			{
				case ServiceControllerStatus.ContinuePending:
					textServiceStatus.Text = "Continue Pending";
					buttonContinue.Enabled = false;
					break;
				case ServiceControllerStatus.Paused:
					textServiceStatus.Text = "Paused";
					buttonPause.Enabled = false;
					buttonStart.Enabled = false;
					break;
				case ServiceControllerStatus.PausePending:
					textServiceStatus.Text = "Pause Pending";
					buttonPause.Enabled = false;
					buttonStart.Enabled = false;
					break;
				case ServiceControllerStatus.StartPending:
					textServiceStatus.Text = "Start Pending";
					buttonStart.Enabled = false;
					break;
				case ServiceControllerStatus.Running:
					textServiceStatus.Text = "Running";
					buttonStart.Enabled = false;
					buttonContinue.Enabled = false;
					break;
				case ServiceControllerStatus.Stopped:
					textServiceStatus.Text = "Stopped";
					buttonStop.Enabled = false;
					break;
				case ServiceControllerStatus.StopPending:
					textServiceStatus.Text = "Stop Pending";
					buttonStop.Enabled = false;
					break;
				default:
					textServiceStatus.Text = "Unknown status";
					break;
			}

		}


		protected void OnSelectedIndexChanged(object sender, System.EventArgs e)
		{
			ServiceController controller =
						(ServiceController)listBoxServices.SelectedItem;
			textDisplayName.Text = controller.DisplayName;
			textServiceType.Text = GetServiceTypeName(controller.ServiceType);
			textServiceName.Text = controller.ServiceName;
			SetServiceStatus(controller);
		}

		protected void buttonCommand_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			ServiceController controller =
							  (ServiceController)listBoxServices.SelectedItem;
			if (sender == this.buttonStart)
			{
				controller.Start();
				controller.WaitForStatus(ServiceControllerStatus.Running);
			}
			else if (sender == this.buttonStop)
			{
				controller.Stop();
				controller.WaitForStatus(ServiceControllerStatus.Stopped);
			}
			else if (sender == this.buttonPause)
			{
				controller.Pause();
				controller.WaitForStatus(ServiceControllerStatus.Paused);
			}
			else if (sender == this.buttonContinue)
			{
				controller.Continue();
				controller.WaitForStatus(ServiceControllerStatus.Running);
			}
			int index = listBoxServices.SelectedIndex;
			RefreshServiceList();
			listBoxServices.SelectedIndex = index;
			Cursor.Current = Cursors.Default;
		}

		protected void buttonExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}
		protected void buttonRefresh_Click(object sender, System.EventArgs e)
		{
			RefreshServiceList();
		}


	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Monroe Community
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions