Noah Noah - 7 months ago 29
Vb.net Question

Background Program Not Looping

I have a program that runs in the background looping to check if a page on the site has been changed. It works once and shows the message box but if I change it again it won't do anything.

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

Answer

Not sure at this moment what is causing it to crash when run outside of the IDE, but try trapping exceptions that are being thrown in the loop. I imagine there's an exception happening, cratering your app. The below catch block is by no means production ready, normally you want to catch specific exceptions in order to handle them effectively, but this is a cheap way to see if an exception is being thrown and what it is at runtime.

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

Alternatively, you could check your event viewer in windows to see if a .net application exception is being logged. Event Viewer > Windows Logs > Application

Comments