.net windows服务的问题

时间:2011-09-19 22:10:26

标签: windows vb.net service

写了一个Windows服务。虽然代码适用于一个简单的表单应用程序,但它不适用于Windows服务。这里;代码

Imports System.Text.RegularExpressions
Imports System.Net.Sockets
Imports System.Net
Imports System.IO

Public Class Service1

Public Shared Function CheckProxy(ByVal Proxy As String) As Boolean
    Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("http://google.com"),    HttpWebRequest)
    myWebRequest.Proxy = New WebProxy(Proxy, False)
    myWebRequest.Timeout = 10000
    Try
        Dim myWebResponse As HttpWebResponse = CType(myWebRequest.GetResponse(), HttpWebResponse)
        Dim loResponseStream As StreamReader = New StreamReader(myWebResponse.GetResponseStream())
        Return True
    Catch ex As WebException
        If (ex.Status = WebExceptionStatus.ConnectFailure) Then

        End If
        Return False
    End Try
End Function


Protected Overrides Sub OnStart(ByVal args() As String)
    System.IO.File.AppendAllText("C:\AuthorLog.txt",
        "AuthorLogService has been started at " & Now.ToString())
    MsgBox("1")
    Timer1.Enabled = True
End Sub

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

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    MsgBox("2")
    ' Check if the the Event Log Exists 
    If Not Diagnostics.EventLog.SourceExists("Evoain Proxy Bot") Then
        Diagnostics.EventLog.CreateEventSource("MyService", "Myservice Log") ' Create Log 
    End If
    ' Write to the Log 
    Diagnostics.EventLog.WriteEntry("MyService Log", "This is log on " & _
    CStr(TimeOfDay), EventLogEntryType.Information)

    Dim ProxyURLList As New Chilkat.CkString
    Dim ProxyListPath As New Chilkat.CkString
    Dim WorkingProxiesFileData As New Chilkat.CkString
    Dim ProxyArray(10000000) As String
    Dim event1 As New Chilkat.CkString
    event1.setString("started")
    event1.saveToFile("B:\serv.txt", "utf-8")
    Dim ns As Integer = 0
    'Read Configuration File
    Dim sFileName As String
    Dim srFileReader As System.IO.StreamReader
    Dim sInputLine As String
    sFileName = "config.ini"
    srFileReader = System.IO.File.OpenText(sFileName)
    sInputLine = srFileReader.ReadLine()
    Dim temp As New Chilkat.CkString
    Do Until sInputLine Is Nothing
        temp.setString(sInputLine)
        If temp.containsSubstring("proxyurllist=") = True Then
            'Read Proxy-URL-List
            ProxyURLList.setString(sInputLine)
            If ProxyURLList.containsSubstring("proxyurllist=") = True Then
                ProxyURLList.replaceFirstOccurance("proxyurllist=", "")
            End If
        ElseIf temp.containsSubstring("finalproxylistpath=") = True Then
            'Read Proxy-List-Path
            ProxyListPath.setString(sInputLine)
            If ProxyListPath.containsSubstring("finalproxylistpath=") = True Then
                ProxyListPath.replaceFirstOccurance("finalproxylistpath=", "")

            End If
        End If
        sInputLine = srFileReader.ReadLine()
    Loop
    '*********Scrape URLs From Proxy-URL-List*********************
    Dim ProxyURLFileData As New Chilkat.CkString
    ProxyURLFileData.loadFile(ProxyURLList.getString, "utf-8")


    Dim MultiLineString As String = ProxyURLFileData.getString

    Dim ProxyURLArray() As String = MultiLineString.Split(Environment.NewLine.ToCharArray, System.StringSplitOptions.RemoveEmptyEntries)
    Dim i As Integer
    For i = 0 To ProxyURLArray.Count - 1

        '********Scrape Proxies From Proxy URLs***********************
        Dim http As New Chilkat.Http()
        Dim success As Boolean
        ' Any string unlocks the component for the 1st 30-days.
        success = http.UnlockComponent("Anything for 30-day trial")
        If (success <> True) Then
            Exit Sub
        End If
        ' Send the HTTP GET and return the content in a string.
        Dim html As String
        html = http.QuickGetStr(ProxyURLArray(i))
        Dim links As MatchCollection
        links = Regex.Matches(html, "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}")
        For Each match As Match In links
            Dim matchUrl As String = match.Groups(0).Value
            ProxyArray(ns) = matchUrl
            ns = ns + 1
        Next
    Next
    '*************CHECK URLs*****************
    Dim cnt As Integer = 0
    For cnt = 0 To 1

        Dim ProxyStatus As Boolean = CheckProxy("http://" + ProxyArray(cnt) + "/")

        If ProxyStatus = True Then
            WorkingProxiesFileData.append(Environment.NewLine)
            WorkingProxiesFileData.append(ProxyArray(cnt))
        End If
    Next
    WorkingProxiesFileData.saveToFile(ProxyListPath.getString, "utf-8")
End Sub
End Class

在Windows服务中我不能做的基本事情是什么?哦,我也在使用chilkat库.. 为什么我不能在OnStart中使用我的所有代码?我这样做了,服务一开始就停止了。 我可以使用除定时器之外的其他东西并进行无限循环吗?

2 个答案:

答案 0 :(得分:0)

作为Windows服务运行通常不会让您看到任何弹出框等,因为没有UI(除非您选中该框以允许与桌面交互)。

尝试在OnStart方法中添加Timer1.Start。然后在你的Timer1_Tick方法中,首先停止计时器,然后在结束时启动它,这样你的Tick方法就不会在你已经开始工作时触发。

答案 1 :(得分:0)

我意识到我(很晚)参加这个派对,但是你用的是什么样的计时器? System.Windows.Forms.Timer设计用于单线程Windows窗体,不适用于Windows服务应用程序。请改为System.Timers.Timer