我正在尝试使用EDT.net.ftp处理FTP传输时在我的VB.net代码中捕获错误。我已经列出了下面的写操作的完整子程序。我已经将网线断开连接到我的仪器以模拟错误57.这可以正常工作,但是当我重新连接并重试程序时卡住并最终给我一个ContextSwitchDeadlock错误。我想我需要在重试之前关闭FTP连接,但是没有得到命令来执行此操作。 如果我尝试在异常中放置FTPprocess.Quit(),我会收到一条错误,指出未声明FTP进程。
任何帮助都将不胜感激。
Private Sub WriteFTPFile()
' APS 15 Aug 2014 Create the script, and run it. This is in 2 stages. 1, create the script, 2 run the ftp script to retrieve the files.
' temp load the parsed data.
Dim ArrayCount As Integer
Dim LocalFile(3) As String
Dim RemoteFile(3) As String
Dim LocalFilePath As String
Dim Path As String
Dim user As String = "username"
Dim password As String = "password"
'Dim LanguageFileName As String
RemoteFile(1) = "IN_Tool.csv"
RemoteFile(2) = "IN_Joint.csv"
'LocalFilePath = "D:\Technologies\Henrob\IP Addresses\192.168.50.200"
LocalFile(1) = "D:\Technologies\Henrob\IP Addresses\192.168.50.200\IN_Tool.csv"
LocalFile(2) = "D:\Technologies\Henrob\IP Addresses\192.168.50.200\IN_Joint.csv"
Path = "192.168.50.200"
' break out the LocalFilePath
LocalFilePath = ""
Dim LocalPath As String() = LocalFile(1).Split(CChar("\"))
ReDim Preserve LocalPath(UBound(LocalPath) - 1)
LocalFilePath = Join(LocalPath, CChar("\"))
' store the current directory
Dim CurrentDir As String = CurDir()
System.IO.Directory.SetCurrentDirectory(LocalFilePath)
' and create the StopRead.txt
Using StopRead As StreamWriter = File.CreateText("StopRead.txt")
StopRead.WriteLine("no text")
StopRead.Flush()
End Using
' Step 2 transfer the files.
Dim Retry As Boolean = True
While Retry
Try
' send the files
Dim FTPprocess As FTPClient = Nothing
FTPprocess = New FTPClient(Path)
'FTPprocess.RemoteHost = Path
FTPprocess.Login(user, password)
FTPprocess.ConnectMode = FTPConnectMode.PASV
FTPprocess.TransferType = FTPTransferType.BINARY
' stop the PLC Polling
FTPprocess.Put("StopRead.txt", "StopRead.txt")
For ArrayCount = 1 To UBound(RemoteFile)
If Len(RemoteFile(ArrayCount)) > 0 Then
'If FTPprocess.Exists(RemoteFile(ArrayCount)) = False Then
FTPprocess.Put(RemoteFile(ArrayCount), RemoteFile(ArrayCount))
'End If
End If
Next
' Restart the PLS Polling
FTPprocess.Delete("StopRead.txt")
FTPprocess.Quit()
' delete the Stop Read file
File.Delete("StopRead.txt")
' everything ok then we can continue
Retry = False
' catch any exceptions, this can be built on as errors are seen
Catch ex As Exception
Select Case Err.Number
Case 5 ' Cannot find file
If MessageBox.Show("Cannot locate file. Ensure File exists, then retry", "Error", MessageBoxButtons.RetryCancel) = DialogResult.Retry Then
Retry = True
Else
Retry = False
End If
Case 57 ' Cannot find Drive Connection
If MessageBox.Show("Cannot locate Drive on " & Path & "." & vbNewLine & "Ensure Drive with this IP Address is attached, then retry", "Error", MessageBoxButtons.RetryCancel) = DialogResult.Retry Then
Retry = True
Else
Retry = False
End If
Case Else
If MessageBox.Show(CStr(Err.Number) & " " & ex.Message, "Error", MessageBoxButtons.RetryCancel) = DialogResult.Retry Then
Retry = True
Else
Retry = False
End If
End Select
End Try
End While
' reset the current directory
System.IO.Directory.SetCurrentDirectory(CurrentDir)
WriteResults = "Completed Write " & Now & vbNewLine 'SR.ReadToEnd 'returns results of the command window
WriteUpdateText()
Dim debugtext As String = "Write " & CStr(Now)
Debug.Print(debugtext)
Timer1.Enabled = True
End Sub