window| Create
The steps for creating and installing services are very similar, regardless of what processing the Windows service performs. Visual Studio provides a Windows Services project type that enables you to quickly create a Windows service that introduces simple steps:
1 new project, in the New Project dialog box, select the Windows Service icon and enter the service name in the dialog name field to determine. Visual Studio displays the Design view of the service.
2 The properties of the service are set: The attributes of the ServiceBase class are as follows:
A.autolog Gets or sets the Boolean value that specifies whether the service automatically reports Start,stop,pause and contunue operations in the time log
B.canhandlepowerevent Gets or sets whether the specified service can correspond to a Boolean value for the power state change, and if the service provides a Onpowreevent method, the property should be set to true.
C.canstop Gets or sets a Boolean value that the service can stop, and set to True if the OnStop method is provided
D.servicename Gets or sets the name of the system-aware service
Write your service information, such as:
Imports system.serviceprocess
Imports System.Threading
Imports System.Data.SqlClient
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Private Shared Bstop as Boolean
Private Mythread as Thread
#Region "Component Designer generated code"
' Required by the Component Designer
Private Components as System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' You can use the Component Designer to modify this procedure.
' Do not modify it using the Code Editor.
Private Sub StartServer ()
Bstop = False
Dim OrderForm, DDH, Matnrh as String
Dim Mylocaldatareader as SqlDataReader
Dim Myprddatareader as SqlDataReader
Dim MYKC as SqlDataReader
Dim KCSL, PRDSL, TEMPSL as Long
Dim flag as Boolean = False
Dim Mat as String
Dim VRM as Long
While not bstop
Try
Sqlcmdtemp.commandtext = "Delete from Tb_ty_cctemp"
Sqlcmdtemp.connection = sqlconnection_local
Sqlcmdtemp.executenonquery ()
If sqlcmdtemp.executenonquery () = 0 Then
Mythread. Sleep (180000)
End If
End While
Mythread. Sleep (180000)
Catch ex as Exception
Bstop = True
End Try
End While
End Sub
Private Sub Stopserver ()
Dim i as Integer = 0
Bstop = True
Try
Sqlconnection_local. Close ()
Scmlocal.commandtext = "INSERT into Myservicelog (vc_status,dt_created) VALUES (' Service shutdown ', GETDATE ())"
Sqlconnection_local. Open ()
Scmlocal.connection = sqlconnection_local
i = Scmlocal.executenonquery
Catch ex as Exception
Bstop = True
End Try
End Sub
Protected Overrides Sub OnStart (ByVal args () as String)
' Add code to start the service here. This method should set the specific action
' So that the service can perform its work.
Dim I as Integer
Try
Sqlconnection_local. Close ()
Scmlocal.commandtext = ""
Scmlocal.commandtext = "INSERT into Myservicelog (vc_status,dt_created) VALUES (' service startup ', GETDATE ())"
Sqlconnection_local. Open ()
Scmlocal.connection = sqlconnection_local
i = Scmlocal.executenonquery
Catch ex as Exception
Bstop = True
End Try
Mythread = New Thread (AddressOf startserver)
Mythread. Start ()
End Sub
Protected Overrides Sub OnPause ()
Mythread. Suspend ()
End Sub
Protected Overrides Sub oncontinue ()
Mythread. Resume ()
End Sub
Protected Overrides Sub OnStop ()
' Add code here to perform the shutdown operation required to stop the service.
Me.stopserver ()
End Sub
End Class
If you need to write the service time to the Windows event log, the following code fragment uses the EventLog object to put an item into the Application log:
Dim log As New EventLog ("Application")
Log.source= "program Name"
Log.writeentry ("message to place into the event log")
Log.close
Add installation
Right-click in Design view, select Add installer and make appropriate settings
Install using the InstallUtil program