0

我有一个程序会不时重启。该程序使用一个计时器来启动一个线程,该线程将文件从文件夹上传到我的 ftp 服务器。但是当 ftp 服务器宕机时,程序仍会根据需要自行重启,但旧进程并没有关闭,而是停留在任务管理器中。当 ftp 恢复时,它开始结束进程。

ftp 服务器在线时不会出现此问题。

当 ftp 服务器脱机时,可能导致进程停留的原因是什么?

要重新启动我只是使用的程序Application.Restart()

当前代码:

Public Sub UploadToFTP()
    Dim request = DirectCast(WebRequest.Create(FtpAdress), FtpWebRequest)
    request.Credentials = New NetworkCredential(_UserName, _Password)
    request.Method = WebRequestMethods.Ftp.ListDirectory
    Try
        Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
            Try
                Dim _FromPath As String
                Dim _ToPath As String
                Dim Dt As New DataTable
                Dim flag As Boolean = False

                'Create a link to an FtpServer
                Dim ftp As New FTPclient(_HostName, _UserName, _Password)
                Dim _dir As New DirectoryInfo(sourceDir)
                ' Upload multiple files
                For Each _file As FileInfo In _dir.GetFiles("*.*")
                    _FromPath = sourceTxt + _file.Name
                    _ToPath = "/UploadedData /" + _file.Name
                    'upload a file
                    flag = ftp.Upload(_FromPath, _ToPath)
                    '' file uploaded then delete
                    If flag Then
                        _file.Delete()
                    End If
                Next
            Catch ex As Exception
            End Try
        End Using
    Catch ex As WebException
        Dim response As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
        'Does not exist
        If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
        End If
    End Try
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    FTPup = New System.Threading.Thread(AddressOf UploadToFTP)
    FTPup.Priority = ThreadPriority.AboveNormal
    FTPup.Start()
End Sub
4

1 回答 1

1

尝试使用FTPup.IsBackground = True它使线程属于应用程序,并应在应用程序关闭时关闭它。

于 2013-07-31T14:08:51.063 回答