后台程序不循环

时间:2016-04-29 19:20:57

标签: vb.net

我有一个在后台循环运行的程序,用于检查网站上的页面是否已更改。它工作一次并显示消息框但如果我再次更改它就不会做任何事情。

Imports System.Net
Imports System.String
Imports System.IO
    Module Main
        Sub Main()
            While 1 = 1
                Dim client As WebClient = New WebClient()
                Dim reply As String = client.DownloadString("http://noahcristinotesting.dx.am/file.txt")
                If reply.Contains("MsgBox") Then
                Dim Array() As String = reply.Split(":")
                MessageBox.Show(Array(2), Array(1))
                Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://noahcristinotesting.dx.am/noahcristinotesting.dx.am/file.txt"), System.Net.FtpWebRequest)
                request.Credentials = New System.Net.NetworkCredential("username", "password")
                request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
                Dim path As String = "C:\test.txt"
                Dim createText As String = "completed"
                File.WriteAllText(path, createText)
                Dim fileftp() As Byte = System.IO.File.ReadAllBytes("C:\test.txt")
                Dim strz As System.IO.Stream = request.GetRequestStream()
                strz.Write(fileftp, 0, fileftp.Length)
                strz.Close()
                strz.Dispose()
            End If
            End While
        End Sub
    End Module

1 个答案:

答案 0 :(得分:1)

目前还不确定在IDE外部运行时导致崩溃的原因是什么,但是尝试捕获循环中抛出的异常。我想在那里发生了一个例外,让你的应用程序萎靡不振。下面的catch块绝不是生产就绪的,通常你想捕获特定的异常以便有效地处理它们,但这是一种很便宜的方法来查看是否抛出异常以及它在运行时是什么。

Sub Main()
    Try
        While 1 = 1
            Dim client As WebClient = New WebClient()
            Dim reply As String = client.DownloadString("http://noahcristinotesting.dx.am/file.txt")
            If reply.Contains("MsgBox") Then
                Dim Array() As String = reply.Split(":")
                MessageBox.Show(Array(2), Array(1))
                Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://noahcristinotesting.dx.am/noahcristinotesting.dx.am/file.txt"), System.Net.FtpWebRequest)
                request.Credentials = New System.Net.NetworkCredential("username", "password")
                request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
                Dim path As String = "C:\test.txt"
                Dim createText As String = "completed"
                File.WriteAllText(path, createText)
                Dim fileftp() As Byte = System.IO.File.ReadAllBytes("C:\test.txt")
                Dim strz As System.IO.Stream = request.GetRequestStream()
                strz.Write(fileftp, 0, fileftp.Length)
                strz.Close()
                strz.Dispose()
            End If
        End While
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

或者,您可以在Windows中检查您的事件查看器,以查看是否正在记录.net应用程序异常。事件查看器> Windows日志>应用