• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create Windows Service C#

abbiepierpoint

Influencer Sponsorship Pro
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
97
Likes
185
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we are going to learn how to create windows service. One of the powerful feature of .NET environment is it is facilitate to create windows services. Windows services are executable applications that run in background. They are controlled by Service Control Manager. According to Microsoft windows service is very much close to unix concept of cron job.
Usually starts when windows starts that’s mean windows service usually start with operating system boot up and run until operating system shut down.

Creating a service

  1. using

    System

    ;
  2. using

    System.ServiceProcess

    ;
  3. System.Configuration

    .

    Install

    .

    Installer

    ;


  4. public

    class

    MyService:

    ServiceBase
  5. {
  6. public

    MyService(

    )
  7. {
  8. this

    .

    ServiceName

    =

    "MyService"

    ;
  9. this

    .

    CanStop

    =

    true

    ;
  10. this

    .

    CanPauseAndContinue

    =

    false

    ;
  11. this

    .

    AutoLog

    =

    true

    ;
  12. }

  13. protected

    override

    void

    OnStart(

    string

    [

    ]

    args)
  14. {
  15. // do startup stuff
  16. }

  17. protected

    override

    void

    OnStop(

    )
  18. {
  19. // do shutdown stuff
  20. }

  21. public

    static

    void

    Main(

    )
  22. {
  23. System.ServiceProcess

    .

    ServiceBase

    .

    Run

    (

    new

    CronService(

    )

    )

    ;
  24. }
  25. }

At the top we have to reference two .net library base class. Actually what each class do is
Service base class provide following methods
1. OnStart:
This method is called by Service Control Manager when your service is started.

2. OnStop:
This method is called by Service control manager when your service is stopped.

3. OnPause:
This method is called when your service is paused.

4. OnContinue:
This method is called when your service resumes after being paused.

5. OnShutdown:
This method is called when the system is shutting down.

Installer class.
This class provides custom installation functionality to application. In theory windows service application cannot be started and stop like usual application so we need to configure service control manager by adding set of installers.

Here is sample installer code

  1. using

    System.ComponentModel

    ;
  2. using

    System.Configuration.Install

    ;

  3. [

    RunInstaller(

    true

    )

    ]
  4. public

    class

    MyServiceInstaller :

    Installer
  5. {
  6. private

    ServiceProcessInstaller processInstaller;
  7. private

    ServiceInstaller serviceInstaller;

  8. public

    CronInstaller(

    )
  9. {
  10. processInstaller =

    new

    ServiceProcessInstaller(

    )

    ;
  11. serviceInstaller =

    new

    ServiceInstaller(

    )

    ;

  12. processInstaller.

    Account

    =

    ServiceAccount.

    LocalSystem

    ;
  13. serviceInstaller.

    StartType

    =

    ServiceStartMode.

    Manual

    ;
  14. serviceInstaller.

    ServiceName

    =

    "MyService"

    ;

  15. Installers.

    Add

    (

    serviceInstaller)

    ;
  16. Installers.

    Add

    (

    processInstaller)

    ;
  17. }
  18. }

How to implement.
Following code shows sample implementation OS onStart method

  1. protected

    override

    void

    OnStart(

    string

    [

    ]

    args)
  2. {
  3. //hur24serviceRef();

  4. _timer.

    AutoReset

    =

    true

    ;
  5. _timer.

    Interval

    =

    1800000

    ;

    // // 30 min
  6. //_timer.Interval = 86400000; // // 24hour
  7. //_timer.Interval = 3600000; // 1hour seconds
  8. _timer.

    Elapsed

    +=

    rtndoc;
  9. _timer.

    Start

    (

    )

    ;
  10. }

  11. private

    void

    rtndoc(

    object

    sender, ElapsedEventArgs e)
  12. {
  13. string

    filepath =

    @"C:\FilepathTest\sample.txt"

    ;
  14. string

    time =

    DateTime.

    Now

    .

    ToString

    (

    )

    ;
  15. System.IO

    .

    StreamWriter

    wri =

    new

    System.IO

    .

    StreamWriter

    (

    filepath, true

    )

    ;
  16. wri.

    WriteLine

    (

    "\n

    "

    +

    time+

    "-dISTRIBUTOR"

    )

    ;
  17. wri.

    Close

    (

    )

    ;

  18. #region pricelist Process

  19. DistributorService.

    Distributor24

    .

    ServiceSoapClient

    priceli =

    new

    Distributor24.

    ServiceSoapClient

    (

    )

    ;
  20. priceli.

    InsertDistributorToKandy

    (

    )

    ;

  21. #endregion

  22. }

  23. protected

    override

    void

    OnStop(

    )
  24. {
  25. _timer.

    Stop

    (

    )

    ;
  26. }

Finally put all these code into file called myService.cs and build it usual way.

How to installed
Following small code shows how to run installed id using CMD

  1. InstallUtil /

    LogToConsole=

    true

    MyService.

    exe

How to start and stop
  1. net start MyService and net stop MyService.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,186

324,194

Top