This project has moved and is read-only. For the latest updates, please go here.

OnProgress event handler in Winform

May 16, 2016 at 6:48 AM
I want to update progress info by using ProgressBar control in winform, because the operation is Cross-Thread so I write like this:
 progressBar1.Invoke
            (
                new MethodInvoker
                (
                    delegate
                    {
                        progressBar1.Value = (progress.TransferredBytes * 100) / progress.TotalBytes;
                    }
                )
            );
but it's doesn't work.

when i change the code to:
progressBar1.BeginInvoke
            (
                new MethodInvoker
                (
                    delegate
                    {
                        progressBar1.Value = (progress.TransferredBytes * 100) / progress.TotalBytes;
                    }
                )
            );
It's work, but some times it will produce "Received message from illegal endpoint" exception.

Can some body to FIX this problem?

by the way I use tftpd32 program as my TFTP server.
May 16, 2016 at 11:00 AM
Hi,

basically your code looks ok. You might want to look into Lambda expressions to shorten it a bit, though.

ITftpTransfer.OnProgress() will be called quite frequently, maybe once every 512 byte. Your first version is blocking and will wait for the progress bar to redraw every time before continuing to sent the next TFTP packet. That will slow down the tftp Transfer significantly.

Your second version does better and offloads the progress bar updates to a second thread.

Performance-wise, I would even suggest a third option:
1) ITftpTransfer.OnProgress(), save the progress in a member variable of your class
2) Use a GUI-timer to update progressBar1.Value from that member variable every 1-2 seconds.

Regarding the error "Received message from illegal endpoint": That's weird, I am not sure why you are seeing this. It's an internal error of the library. Do you also see this error when you run your program without the progress bar update?

Best regards, Michael
May 18, 2016 at 1:11 AM
Thank you for your reply.
"Do you also see this error when you run your program without the progress bar update?" No, I haven't saw this error.
I will try your third option .