DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Windows Services

Michael Georgiou user avatar by
Michael Georgiou
·
Oct. 25, 12 · Interview
Like (0)
Save
Tweet
Share
3.55K Views

Join the DZone community and get the full member experience.

Join For Free

This article was written by the Imaginovation team.  They are a Raleigh web design and software development company who uses .NET, PHP, HTML5, JavaScript, and jQuery technologies.

Microsoft Windows services are long-running executables that perform specific functions in their own Windows sessions and do not require user intervention. They can be configured to start automatically whenever the system starts or can be started manually. These features make services ideal for use on a system user need long-running functionality without intervention. Windows services can be associated to particular user on the system.
Windows Services are used in following scenarios:

    •    Wait for incoming requests and Monitor a queue, file system, watching a folder or time event call etc.
    •    If a program just needs to run periodically, like on every hour. It is normally easier to create a scheduled task using windows service.

Windows Service runs in the background, withoutuser interaction.
Here are steps to create a windows service:

    •    Open Visual Studio -> Click on File -> New Project.
    •    Select language -> Click on Windows -> Windows Service
    •    It will create a new project with service.vb file
    •    Click on save 
    •    And Service.vb file will have following code

Public Class Service1

    Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
    End Sub

    Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
    End Sub

End Class

   •    Add following code to service.vb file

Imports System.IO

Public Class Service1

    Protected Overrides Sub OnStart(ByVal args() As String)
       ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        EventLog.WriteEntry("Service started......")
        File.Copy("c:\test.txt", "d:\test.txt")
        File.Delete("c:\test.txt")
        EventLog.WriteEntry("File moved.")
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        EventLog.WriteEntry("Service stopped......")
    End Sub

End Class 

    •    Now we need to add installer. Go to design view of service.vb and right click then select add installer
    •    Now projectInstaller.vb file got added into project. Open design view and set properties for both ServiceProcessInstaller1 and ServiceInstaller1 as mentioned below. 
    •    Now we need to add setup project for windows service. Go to file -> Add  -> New Project
    •    Then go to Other Project types -> Setup and Deployment  -> Setup Project . Give some name to project and click OK. 

   •    Now we need to add Service.vb to the setup project. Right click on setup project and select Add  -> Project Output 
    •    Then  select TestWindowsService from Project dropdown and select Primary Output then click OK.
    •    Now we need to add custom action to the setup project. Right click on setup project and select Custom actions. Then right click on Custom Actions and click on Add Custom Action. Then double click on Application folder and select Primary Output from TestWindowsService. Then click OK and Rebuild the project.
    •    Now go into service location and double click on setup file. Then click next, Select Everyone and Click next. Service will get installed then finally click Close.
    •    Finally go to services and right click on service then click start. Service will get started.

Web Service

Published at DZone with permission of Michael Georgiou. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AIOps Being Powered by Robotic Data Automation
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • Best Practices for Writing Clean and Maintainable Code
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: