Discuss (FTP), (FTP on .NET CF) and (FTPS, SFTP and SCP).
no avatar





Tue Sep 02, 2014 8:24 pm

Getting .NET - ContextSwitchDeadlock was detected errors.

by apaulsavage » Tue Sep 02, 2014 8:36 pm

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\"
LocalFile(1) = "D:\Technologies\Henrob\IP Addresses\\IN_Tool.csv"
LocalFile(2) = "D:\Technologies\Henrob\IP Addresses\\IN_Joint.csv"
Path = ""

' 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()

' and create the StopRead.txt
Using StopRead As StreamWriter = File.CreateText("StopRead.txt")
StopRead.WriteLine("no text")
End Using
' Step 2 transfer the files.
Dim Retry As Boolean = True
While Retry
' 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
' Restart the PLS Polling

' delete the Stop Read file
' 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
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
Retry = False
End If
Case Else
If MessageBox.Show(CStr(Err.Number) & " " & ex.Message, "Error", MessageBoxButtons.RetryCancel) = DialogResult.Retry Then
Retry = True
Retry = False
End If
End Select

End Try
End While

' reset the current directory
WriteResults = "Completed Write " & Now & vbNewLine 'SR.ReadToEnd 'returns results of the command window

Dim debugtext As String = "Write " & CStr(Now)
Timer1.Enabled = True

End Sub

Who is online

Users browsing this forum: No registered users and 1 guest

Powered by phpBB ® | phpBB3 Style by KomiDesign