vb.net中windows服務的建立

來源:互聯網
上載者:User
window|建立  

無論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程式進行安裝



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.