Tutorial: creating a Windows (NT) Services

Source: Internet
Author: User
Http://www.c-sharpcorner.com/2/window_service.asp

Tutorial: creating a Windows (NT) Services
Author Date of submission User level
Mahesh Chand 01/19/2000 Intermediate

Tools used Visual C #. net
Downloads Project: mcwinservice.zip 124 KB

Note:OK, I am being lazy here. The project name in this sample code isMcwebserviceWhich is a spell mistake. I meant to putMcwinservice. And now I don't want to change all the screen shots and code all over again. I hope it won't confuse you :).

OK, its time for one more tutorial. this times pick is Windows Services. creating Windows Services is not a big deal using Visual C #. just follow few simple steps and you are all set to run and test your first Windows service.

Windows ServicesIs new name for NT services you used to develop in previous versions of Visual Studio. this tutorial walks you through how to create and use your Windows Services. this service writes some text to a text file when stop and start the service. the base idea is taken from msdn but its more elaborated. you can modify it according to your needs.

Step 1. Create skeleton of the service

To create a new window service, pick Windows Service option from your Visual C # projects, give your service a name, and click OK.

The result look like this. The wizard adds webservice1.cs class to your project.

Set yourServicenameTo your own name so it wocould be easier to recognize your service during testing or you can set this property programmatically using this line this. servicename = "mcwinservice ";

This is the name you will be looking for later :).

The Default Code of webservice1.cs added by the wizard looks like here

Namespace mcwebservice

{

Using system;

Using system. collections;

Using system. core;

Using system. componentmodel;

Using system. configuration;

Using system. Data;

Using system. Web. Services;

Using system. diagnostics;

Using system. serviceprocess;

Public class winservice1: system. serviceprocess. servicebase

{

/// <Summary>

/// Required designer variable.

/// </Summary>

Private system. componentmodel. Container components;

Public winservice1 ()

{

// This call is required by the winforms component designer.

Initializecomponent ();

// Todo: add any initialization after the initcomponent call

}

// The main entry point for the Process

Static void main ()

{

System. serviceprocess. servicebase [] servicestorun;

// More than one user service may run within the same process. To add

// Another service to this process, change the following line

// Create a second service object. For example,

//

// Servicestorun = new system. serviceprocess. servicebase [] {New winservice1 (), new myseconduserservice ()};

//

Servicestorun = new system. serviceprocess. servicebase [] {New winservice1 ()};

System. serviceprocess. servicebase. Run (servicestorun );

}

/// <Summary>

/// Required method for designer support-do not modify

/// The contents of this method with the code editor.

/// </Summary>

Private void initializecomponent ()

{

Components = new system. componentmodel. Container ();

This. servicename = "winservice1 ";

}

/// <Summary>

/// Set things in motion so your service can do its work.

/// </Summary>

Protected override void onstart (string [] ARGs)

{

// Todo: Add code here to start your service.

}

/// <Summary>

/// Stop this service.

/// </Summary>

Protected override void onstop ()

{

// Todo: Add code here to perform any tear-down necessary to stop your service.

}

}

}

Step 2. add functionality to your service

As you saw webservice1.cs, there are two overridden funden onstart and onstop. the onstart function executes when you start your service and the onstop function gets execute when you stop a service. I write some text to a text file when you start and stop the service.

Protected

Override void onstart (string [] ARGs)

{

Filestream FS =

NewFilestream (@ "C: \ temp \ mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );

Streamwriter m_streamwriter =

NewStreamwriter (FS );

M_streamwriter.basestream.seek (0, seekorigin. End );

M_streamwriter.writeline ("mcwindowsservice: Service started \ n ");

M_streamwriter.flush ();
M_streamwriter.close ();

}

/// <Summary>

/// Stop this service.

/// </Summary>

Protected override void onstop ()

{

Filestream FS =

NewFilestream (@ "C: \ temp \ mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );

Streamwriter m_streamwriter =

NewStreamwriter (FS );

M_streamwriter.basestream.seek (0, seekorigin. End );

M_streamwriter.writeline ("mcwindowsservice: Service stopped \ n ");

M_streamwriter.flush ();
M_streamwriter.close ();

}

 

Step 3: Install and run the service

Build of this application makes one EXE, mcwinservice.exe. You need to call installutil to register this service from command line.

Installutil c: \ mcwebservice \ bin \ debug \ mcwebservice.exe

You use/u option to uninstall the service.

Installutil/U c: \ mcwebservice \ bin \ debug \ mcwebservice.exe

Run the application

Step 4: start and stop the service

You need to go to the computer management to start and stop the service. You can use manage menu item by Right clicking on my computer.

UnderServices and Applications, You will see the serviceMcwinservice. Start and Stop menu item starts and stops the service.

Step 5: test the service

Go to your temp directory and see if text file is there with contents or not.

That's it.

Feedback: Please post all of your feedback on C # corner discussion forums.

References:

    • Msdn

 

About the author

Mahesh Chand is. net consultant, author and the admin and founder of C # corner. he has been working. NET technology since pre beta releases. mahesh's background primary des Master's in computer science and applications and B. SC. mathematics. he has over 8 years of software development experience with Microsoft technologies and has extensive experience developing client-server, distributed, Web Services, and component based applications using Visual Studio. net. before moving. net, Mahesh has worked on MFC, COM, ATL, and Visual C ++ based applications. Mahesh has helped companies in migration of their existing non. net products. net products and build new products based on. NET technology. if you are looking for a consulting help, contact him at mahesh@c-sharpcorner.com.

Publication:Mahesh Is a MCP and author of several. NET Programming books includingGraphics programming with GDI + Addison Wesley 2003, Applied ADO. Net: building data-driven solutions Apress 2003, a programmer's Guide to ADO. Net in C # Apress 2002, and the complete guide for Visual C # programmers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.