Ibraheem Osama Mohamed (ابراهيم اسامه محمد)

Welcome to my blog :)

Windows Service

leave a comment »

How is the big programs works ?? How I can make a big application that auto update it self ?? watch the process of the computer ?? mange it self ?? many many questions that Windows Service Answer it.

Windows Service is a Windows Application enable you to perform tasks that execute as different back-ground processes “Without User Interface”.Windows Service performs tasks such as monitoring the usage of a database,managing networks connection and collect and analyze system usage data and log events in the system.

Windows Services manged by the Service Control Manger (SCM) the .NET framework enables you to implement SCM in your code so you can create,install and control Windows Service.

The Main three Components of the Windows Service are

1)Service Application : An application that consists of one or more services that provide the desired functionality.
2)Service Controller Application : An application that enables you to control the behavior of a service.
3)Service Control Manager : A utility that enables you to control the services that are installed on a computer.

Differences between service application and other application

1)Windows Service need to be installed “I’ll mention how later”.
2)Windows Service can’t be  debugged.
3)When Executing the Windows Service the run method loads a service into the SCM and if there are an error it’s not displayed it logged in the event logs.

Let’s get in the interesting part How to make a Windows Service application

1)Open Visual Studio then Create New Project.

2)Select Windows Service. “Visual Studio will create the project and you’ll find that there are more than one class”.

3) In the ServiceClass.cs[Design] you must add Installer so that you can install your application on your system
right click in the service design window then add installer.

4)Now you create the installer, you must specify the properties of your installer”Project Installer” open the installer then Click On serviceInstaller and serviceProcessInstaller to choose the properties you want. but the most important is the Account property in the serviceProcessInstaller. you can get more info from this link http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceprocessinstaller.aspx.

5)Open the service Class and then view the code you’ll find the contractor , a OnStart method and OnStop method write the code you want in them, don’t forget that you can override other methods like OnContinue.

6)After you end your project build it, if you run it nothing will happen.

7)Now Install your service. “Open the command line as administrator then use the installutil command to install you service”
Installutil windowsService1.exe
If you did’t put the VS in your environment variables open the VS command line but don’t forget as administrator.

8)After you install your service if you choosed user in the serviceProcessInstaller’s Account property you’ll find a window that requires the username and the password of you computer account to proceed installation, else the program will be installed.

9)Now Go to the your system services”Right Click on Computer ,Mange ,Services and Applications then Services” find your service and right click on it then choose start.

Now your service is running but take care that the service can’t interact with the UI directly if you want to make the application interact with the UI this link will help you.
http://devcity.net/Articles/74/1/servicecontroller.aspx
http://msdn.microsoft.com/en-us/library/ms683502(v=VS.85).aspx

You can but this lines of code in the OnStart method so that you can check if your service is running or not.

protected override void OnStart(string[] args)
{

FileStream fs = new FileStream(@”d:\mcWindowsService.txt”,
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(” mcWindowsService: Service Started \n”);
m_streamWriter.Flush();
m_streamWriter.Close();

}

Thanks for reading but you can make bad thing from windows service 🙂 I hope that you are not one of those 🙂
References I used MCAD/MCSD  Microsoft.

Ibraheem Osama Mohamed
Mobile Developer-Rashdan It || Microsoft Student Partner -Microsoft Egypt
Twitter : @IbraheemOsamaMo

Advertisements

Written by Ibraheem Osama Mohamed

March 18, 2012 at 11:41 pm

Posted in Windows 8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: