我有一个程序偶尔会重新启动。该程序使用一个计时器,它启动一个线程,将文件从文件夹上传到我的ftp服务器。但是,当ftp服务器出现故障时,程序仍然会根据需要重新启动,但是旧进程不会关闭并留在任务管理器中。当ftp恢复时,它开始结束进程。
当ftp服务器处于联机状态时,不会出现此问题。
当ftp服务器脱机时,什么会导致进程停留?
要重新启动程序,我只需使用Application.Restart()
当前代码:
代码语言:javascript运行复制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