banner



How Do I Create A System Service Using Vb

Environment: .Net, Visual Basic

Introduction

You want to write an awarding that constantly monitors some files, creates a log, or whatsoever, and one that ran constantly in the background while the machine is busy doing something else. The best mode to practise this was to run a Windows application continuously or at a regular interval with the employ of the Windows scheduler. But there was i big disadvantage: Someone had to log on to the organisation to kickoff this application.

This is no longer a trouble because the all-time way to do all this is to create a Windows Service for Windows 2000/Windows NT.

What Is Windows Service (Previously Called NT Service)?

The core function of a Windows Service is to run an application in the background. A few things that brand them different from a Windows awarding are that a Windows Service starts before any user logs on to the system (if information technology has been set up to offset at boot up). The service can be set up in such a way that it requires the user to start it manually.

Too, Windows Service has its own process; hence, it runs very efficiently. Normally, a Windows Service will not have a user interface for the simple reason that it tin exist run fifty-fifty if no one is logged on to the system. This is non a rule; yous can nonetheless take a Windows Service with a user interface.

In Windows 2000, you can view all the services running on your computer by opening Command Panel, Administrative Tools, and and so clicking Services.

Creating a Windows Service in VB.NET

Prior to VB.Cyberspace, creating a Windows Service was a lot of work. You lot had to use some system-level process that was non very easy, only cheers to VB.NET this has become very easy. We shall now learn how to create a Windows Service.

You should know a few things before we dive in. Windows Service is not available in Windows 95, 98, or ME. Y'all demand to accept Windows NT or Windows 2000 to run Services.

The advantage to using .NET is that the framework incorporates all the classes. This shall help us to create, install, and control Windows Service.

Open up your Visual Studio .Net, create a new Windows Service Project, which we shall call MyService, and then click OK.

Add the Timer control from the Toolbar in the Components tab. In the properties window of Timer1, modify the Interval property to 10000, which is ten seconds.

The Source Code

Double-click the Timer1 control to open up a code window for Timer1_Elapsed and blazon the following lawmaking. (This code shall execute every 10 seconds.)

          Dim MyLog As New EventLog()    If Not MyLog.SourceExists("MyService") Then     MyLog.CreateEventSource("MyService", "Myservice Log")      End If   MyLog.Source = "MyService"      MyLog.WriteEntry("MyService Log", "This is log on " & _                     CStr(TimeOfDay), _                     EventLogEntryType.Data)        

Type the following code in the OnStart procedure. (This procedure is called when you first the service, which shall enable the timer.)

          Timer1.Enabled = True        

And type the following code in the OnStop procedure. (This process is called when yous terminate the service, which shall disable the timer.)

          Timer1.Enabled = Faux        

Our awarding is now ready, but a few things withal need to be done before nosotros move ahead. When we build this application, the executable created is non a Windows awarding; hence, you can't but click and run it. Information technology needs to be installed as a service but don't worry; we don't take to practice it manually. VB.Net has a facility to add an installer to our program and then employ a utility to install the service.

Adding Installer to the Project

Open up the Service1.vb pattern window and right-click and and so select the Add Installer option. This adds an installer project, ProjectInstaller.vb, with two controls, ServiceProcessInstaller1 and ServiceInstaller1, in our existing projection.

Select the ServiceInstaller1 control and open the property window. Alter the ServiceName and DisplayName backdrop to MyService. (This is the name you desire to appear in the listing of services in Services windows.)

Select the ServiceProcessInstaller1 control and open up the property window. Change the Account property to LocalSystem. (This needs to be specified because nosotros need to run this on our local machine.)

Now information technology time to build the application and create an executable. Select Build Solution from the Build menu to create an executable with installation instructions for the service.

Installing the Service

To install this service, we demand to utilize the InstallUtil plan, which is a .Net utility, to install Windows Service. You can find this in folder C:\WINNT\Microsoft.NET\Framework\v1.0.3705. (This might be different on your computer, depending upon the version you are using.)

Alternatively, you could run the .Internet Control Window past selecting Starting time, Programs, Microsoft Visual Studio .Cyberspace, Visual Studio .Net Tools, Visual Studio .Net Command Prompt. This sets all the required paths for you.

Type the following control in that window: InstallUtil C:\tgol\Myservice\Bin\Myservice.exe. (This is the path for the executable of the service we but created. Call back, VB.NET created the executable in the Bin Binder under the projection folder. Delight make sure you change this to your executable path.) Please look at the message and log to make certain the service is installed properly.

Starting the Service

Running a service and starting a service are two different things. When we install the service with InstallUtil, we are now running the service but take nonetheless to start information technology. To view and beginning the service, open up the Command Console. Open up Administrative Tools, click Services, locate MyService, and correct-click and select Start to start this service.

Now our service has started. Open the Issue Viewer from Authoritative Tools and click the Awarding Log to meet the logs created past the Service (MyService) every 10 seconds. (If you don't meet any logs, click Refresh [F5]. Y'all will have to keep refreshing to encounter the latest event logs.

Stopping the Service

Open Control Panel, open Administrative Tools, click Services, right-click MyService, and select Stop to terminate the service.

Uninstalling the Service

This process is similar to installing the service, simply now we shall run the InstallUtil with /U Parameter, which uninstalls the service:

InstallUtil /U C:\tgol\Myservice\Bin\Myservice.exe (or executable path on your computer). Please wait at the message to ostend that the service is uninstalled properly.

Tips

  • Stop the service and close the Service window before you lot install/uninstall the service.
  • Always uninstall/install if you make any changes to the service application.
  • Try fugitive user interface, inputs, and msgbox in the Service Awarding.
  • Open ProjectInstaller.vb, select ServiceInstaller1 control, and open up the property window. Change the StartType holding to Automated if you lot want to start the service automatic.

Determination

Windows Service is often overlooked. Use Windows Service instead of a standard application when you accept to monitor or administrate something in the background. The simply disadvantage is the installation procedure, just I bet in that location are more positive aspects and so negative when selecting Windows Service over applications running Windows Scheduler.

Source: https://www.codeguru.com/visual-basic/creating-a-windows-service-in-vb-net/

Posted by: sieverswoudde89.blogspot.com

0 Response to "How Do I Create A System Service Using Vb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel