2013-07-31 66 views
0

我有一段時間會重新啓動自己的程序。該程序使用一個定時器啓動一個線程,將一個文件夾中的文件上傳到我的ftp服務器。但是,當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 

回答

1

嘗試使用FTPup.IsBackground = True這使得線程屬於該應用程序,並應關閉的應用程序關閉。

+0

謝謝,它的工作原理。 它現在幾秒鐘​​後關閉舊的過程。 – rip2444

相關問題