Search code examples
vb.netwindowsbackgroundworkerfreezeui-thread

Application momentarily hangs main thread


I am trying to connect to a SFTP server using WinSCP and vb.net. Everything goes well and the file uploads/downloads correctly but after it has finished, if I interact with the form in any way (ie. Pressing a button) the main thread hangs for about 10-15 seconds. Then it resumes and never hangs again unless I restart the application. This seems strange to me because I am doing all WinSCP functions on a separate thread using a BackgroundWorker. I am using mySession.putFiles for uploading and GetFiles for downloading. This seems to only happen when downloading/uploading files.

Here is a snippet of my code which is based on This Example:

Using mySession As Session = New Session
' Connect
mySession.Open(mySessionOptions)

' Upload files
Dim myTransferOptions As New TransferOptions
myTransferOptions.TransferMode = TransferMode.Binary

Dim transferResult As TransferOperationResult
transferResult = mySession.GetFiles("KidsBank/FVbal.txt", "FVbal.txt") 

transferResult.Check()
ConsolePrint("Got fv")
transferResult = mySession.GetFiles("KidsBank/FERbal.txt", "FERbal.txt")
transferResult.Check()
ConsolePrint("Got fer")
transferResult = mySession.GetFiles("KidsBank/AZbal.txt", "AZbal.txt")
transferResult.Check()
ConsolePrint("Got az")

LoadingText.Text = "Downloading Check Dates..."

transferResult = mySession.GetFiles("KidsBank/FVlastcheck.txt", "FVlastcheck.txt") 
transferResult.Check()
ConsolePrint("Got fv check")
transferResult = mySession.GetFiles("KidsBank/FERlastcheck.txt", "FERlastcheck.txt")
transferResult.Check()
ConsolePrint("Got fer check")
transferResult = mySession.GetFiles("KidsBank/AZlastcheck.txt", "AZlastcheck.txt")
transferResult.Check()
ConsolePrint("Got az check")

Solution

  • Removing the ConsolePrint command was the issue since I was using CheckForIllegalCrossThreadCalls in False for debugging.