Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
7.8k views
in .NET FTP by (1.9k points)
Can you please help post some code to explain how one can calculate speed per second. I know that to do that you need to handle the bytestransferred event by getting the total sent one second ago and compare it with the total sent now or something. Please help.

Thanks

12 Answers

0 votes
by (51.6k points)
Create an ExFTPConnection (or FTPConnection or SecureFTPConnection) and add handlers for the Downloading event (to store the start-time) and the BytesTransferred event (to calculate the download speed).
ExFTPConnection exFTP = new ExFTPConnection();
exFTP.Downloading += new FTPFileTransferEventHandler(exFTP_Downloading);
exFTP.BytesTransferred += new BytesTransferredHandler(exFTP_BytesTransferred);

Store the start-time and store in a field.
void exFTP_Downloading(object sender, FTPFileTransferEventArgs e)
{
    downloadStartTime = DateTime.Now;
}

Calculate the download speed and print it to the console.
void exFTP_BytesTransferred(object sender, BytesTransferredEventArgs e)
{
    TimeSpan downloadTime = DateTime.Now - downloadStartTime;
    float downloadSpeed = ((float)e.ByteCount) / downloadTime.TotalSeconds;
    Console.WriteLine("Average download speed = " + downloadSpeed + " bytes per second.");
}


downloadStartTime is a DateTime field of the class.

- Hans (EnterpriseDT)
0 votes
by (1.9k points)
Thank you very much. This is exactly what I was looking for. One more question here please. I am planning to use asynch methods for uploading to several FTP sites at the same time using the build in queuing operation . Now I know how to calculate the speed for each connection independently. What do you think is the best technique to get a total of all the uploads that are in progress.

Thanks
0 votes
by (51.6k points)
You'd need a member variable, totalByteCount, that stores the total number of bytes uploaded so far. Each BytesTransferred handler should update totalByteCount. You should put a 'lock' block around any access to this variable since multiple threads will be accessing it simultaneously.

totalByteCount can then be used in place of e.ByteCount in the same formula that I gave in the previous post. The startTime will also need to be initialized differently since you want it set only for the first upload and not for every upload. You may as well remove it from the Uploading event-handler and simply set it before you launch the whole process.

- Hans (EnterpriseDT)
0 votes
by (1.9k points)
Thanks for your explanation Hans. I will give it a try, thanks
0 votes
by (1.9k points)
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
0 votes
by (162k points)
Presumably your download is on the new thread and the handler is on the main thread.
0 votes
by (1.9k points)
That's what I thought too. But I posted my code because I am stuck and do not know how I can do that. Can you help please?

Thanks
0 votes
by (51.6k points)
Hi leedo

We have been communicating with you for several months now and have written several dozen messages across a broad range of topics, but we have no idea who you are. Some of your questions related to the Express version of edtFTPnet, so I guess you must be a customer. Could you please tell me which company you are from so that we can locate your license and determine whether you are actually entitled to the large amount of support that you are receiving?

- Hans (EnterpriseDT)
0 votes
by (1.9k points)
Hello Hans,

Yes, you are right. I have been extensively experiencing with all version of edftpnet. I tested the free version as well as the trial versions of the Express and Pro.
I resorted to using the free version because the the async methods presented in the Express versions did not halp me achieve the control during multi threaded application that I was looking for. Concerning your question, it makes me feel pretty awkward. Yes, you guys helped me a lot although I am not actually a paying customer. I apologize if was much of nag but I suppose the forums are for non-paid customers. However, I liked the application a lot and I believe we worked together a couple of months ago on enhancing features in the Express version, like the run-time FTPsynchronization. Anyhow, I hope I can still be an active member in your forums. Concerning the problem I have I can't seem to figure out yet how to put the thread on which the bytestransferred event is being fired to sleep.

Thanks
0 votes
by (1.9k points)
Hi,

It is true. I used the Threads Windows view in .NET IDE and found out that the BytesTransferred event is in fact raised in another thread that is different from the thread on which the uploadFile is being called on. I suppose it is now clear why the sleep instruction is not affecting the actual UploadFile method.

Any help?

Thanks

Categories

...