Hi All,
I am currently trying to trap errors within my VB.net code when dealing with FTP Transfer using EDT.net.ftp. I have listed the complete subroutine for the write operation below. I have disconnected the network cable to my instrument to simulate error 57. this works correctly, but when I reconnect and retry the program gets stuck and finally gives me a ContextSwitchDeadlock error. I think I need to close the FTP connection before retrying it, but haven't got the command to do this.
If I try to place FTPprocess.Quit() within the exceptions I get an error stating the FTPprocess is not declared.
Any help here would be appreciated.
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