vb.net中windows服务的创建

 

无论windows服务执行什么样的处理,创建与安装服务的步骤都十分相似.visual studio提供了windows services项目类型,利用它可以迅速创建windows服务,一下介绍简单的步骤:

1)新建项目,在new project对话框中,选择windows服务图标,在对话框name字段输入服务名称,确定.visual studio将显示服务的设计视图.

2)在服务属性里面进行设置:servicebase类的属性如下:

              a.autolog  获得或设置指定服务是否在时间日志中自动报告start,stop,pause和contunue操作的布尔值

              b.canhandlepowerevent  获得或设置指定服务是否可以对应于电源状态变化的布尔值,如果服务提供了onpowreevent方法,该属性应设置为true,

              c.canstop  获得或设置服务是否可以停止的布尔值,如果提供了onstop方法,则设置为true

              d.servicename  获得或设置使系统识别服务的名称

编写你的服务信息,例如:

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 " 组件设计器生成的代码 "

   

    '组件设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是组件设计器所必需的
    ' 可以使用组件设计器修改此过程。
    ' 不要使用代码编辑器修改它。
  
    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('服务关闭',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)
        ' 在此处添加启动服务的代码。此方法应设置具体的操作
        ' 以便服务可以执行它的工作。
        Dim i As Integer
        Try
            SqlConnection_local.Close()
            SCMlocal.CommandText = ""
            SCMlocal.CommandText = "insert into MyServiceLog(vc_status,dt_created) values('服务启动',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()
        ' 在此处添加代码以执行停止服务所需的关闭操作。
        Me.stopserver()
    End Sub

End Class

如果需要把服务时间写入windows事件日志,以下的代码片断使用eventlog对象把一个项放入application日志中:

dim log as new eventlog("application")

log.source="program name"

log.writeentry("message to place into the event log")

log.close

添加安装

在设计视图右击,选择添加安装程序并进行相应的设置

使用installutil程序进行安装

 

 

你可能感兴趣的:(vb.net,windows,vb.net,exception,integer,application,components)