Hi,
I am using the calculation formula as described and it is working just fine. I also wanted to control the speed and apply a small speed control logic. So I added the following:
Private Sub Bytes_Transferred(ByVal sender As Object, ByVal e As BytesTransferredEventArgs)
downloadTime = DateTime.Now - downloadStartTime
downloadSpeed = CSng(e.ByteCount) / downloadTime.TotalSeconds
Dim CurrentSpeedKbps As Single = (downloadSpeed * 8) / 1024
Console.WriteLine("Speed average = " + CurrentSpeedKbps.ToString + " Kbps.")
''Limit upload speed
If CurrentSpeedKbps > 256 Then
Threading.Thread.Sleep(500)
End If
End Sub
This worked correctly and the speed was brought down. However, when I did that in a new thread, the speed was not affected and the file transfer kept going on normally. What is even more strange that if I set the sleep wait time to a long duration like 500000, the transfer still proceeds!!.
Here is my code:
Private Sub UploadFile(ByVal FileName As Object)
Dim ftpConn As New EnterpriseDT.Net.Ftp.ExFTPConnection
AddHandler ftpConn.BytesTransferred, AddressOf Bytes_Transferred
With ftpConn
.UserName = "ftp"
.Password = "pass"
.ServerAddress = "192.168.5.33"
.ConnectMode = EnterpriseDT.Net.Ftp.FTPConnectMode.PASV
.Timeout = 60000
'.TransferBufferSize = 512000
'.TransferNotifyInterval = 102400
.Connect()
.ChangeWorkingDirectory("/myFiles/tmp")
End With
downloadStartTime = DateTime.Now
ftpConn.UploadFile(FileName, IO.Path.GetFileName(FileName))
ftpConn.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileDialog As New OpenFileDialog
FileDialog.ShowDialog()
Dim Pt As New ParameterizedThreadStart(AddressOf UploadToDMP)
Dim t As New Thread(Pt)
t.Start(FileDialog.FileName)
End Sub
I think I know why this is happening but I can't find how to fix it. I believe this is caused by the fact that the thread.sleep is being called onto the wrong thread. If yes, how can I fix that?
Thanks alot