login about faq

How can I pull the bytes per sec transfer speed when using DownloadAsync? I had iterated through the http eventlog before for "RecvBytesPerSec", but this does not seem to exist in the task ProgressInfo

asked Nov 03 '15 at 16:23

Maury's gravatar image

Maury
1


The Async methods should still fire events, so you can get the SendRate/ReceiveRate events, but beware because the events will be called in the background thread. In other words, when your event handler is called, it's in a background thread and you cannot just update a UI element. If you need to update UI elements from the bg thread, use MethodInvoker. See http://www.cknotes.com/vb-net-taskcompleted-event-for-asynchronous-method-call/

link

answered Nov 03 '15 at 19:17

chilkat's gravatar image

chilkat ♦♦
11.8k316358420

See below please, I had to mark as an answer instead of a comment because to many characters. :)

(Nov 04 '15 at 09:00) Maury

Hello, Thanks for responding, however I am not seeing any events get invoked.

    public bool Download(string url, string destinationPath, bool unzip)
    {

        _http.KeepEventLog = true;
        _http.EnableEvents = true;
        _http.OnReceiveRate += _http_OnReceiveRate;
        Task task = _http.DownloadAsync(url, destinationPath);
        task.OnProgressInfo += task_OnProgressInfo;
        task.OnPercentDone += task_OnPercentDone;
        task.KeepProgressLog = true;
        task.EnableEvents = true;

        task.Run();

        while (!task.Finished)
        {

            while (task.ProgressLogSize > 0)
            {
                string logName = task.ProgressInfoName(0);
                string logValue = task.ProgressInfoValue(0);
                task.RemoveProgressInfo(0);
                Debug.Print(logName);
                if (logName.Contains("RecvBytesPerSec"))
                {
                    double bytesPerSec;
                    if (double.TryParse(logValue, out bytesPerSec) == false)
                        bytesPerSec = 0;

                    string transferSpeed = ConvertBytesToTransferSpeed(bytesPerSec);
                    Debug.Print("Speed: " + transferSpeed);
                }
                else if (logName.Contains("PercentDone"))
                {
                    double percentDone;
                    if (double.TryParse(logValue, out percentDone) == false)
                        percentDone = 100.0;

                    Debug.Print("Percent: " + percentDone);
                }
            }

            task.SleepMs(100);
        }

        return true;

    }

    void _http_OnReceiveRate(object sender, DataRateEventArgs args)
    {
        Debug.Print(args.BytesPerSec.ToString());
    }

    void task_OnPercentDone(object sender, PercentDoneEventArgs args)
    {
        Debug.Print(args.PercentDone.ToString());
    }

    private void task_OnProgressInfo(object sender, ProgressInfoEventArgs args)
    {
        Debug.Print(args.Name + " " + args.Value);
    }
link

answered Nov 04 '15 at 08:54

Maury's gravatar image

Maury
1

Try registering the event on the http-object instead of the task-object.

link

answered Jan 09 at 07:40

Stefan%20O's gravatar image

Stefan O
173

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×186

Asked: Nov 03 '15 at 16:23

Seen: 673 times

Last updated: Jan 09 at 07:40

powered by OSQA