1

I'm trying to write to my uploadProgress progress bar's toolTip the current uploaded amount, so when the user mouseovers the progress bar, the can see the tooltip changing showing the uploaded amount against the file size. the code I have so far give me the "busy" icon when I mouse over, until the file has finished uploaded, and then it shows the downloaded amount and file size.

Could someone help me get this working ?

private void uploadFile()
{
    try
    {
        richTextBox1.AppendText("\n\nStarting file upload");
        FtpWebRequest request =
            (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/public_html/test.htm");

        request.Credentials = new NetworkCredential("username", "password");

        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = true;

        request.Method = WebRequestMethods.Ftp.UploadFile;

        using (Stream fileStream = File.OpenRead(@"C:\path\testfile.UPLOAD"))
        using (Stream ftpStream = request.GetRequestStream())
        {
            uploadProgress.Invoke(
                (MethodInvoker)delegate {
                    uploadProgress.Maximum = (int)fileStream.Length; });

            byte[] buffer = new byte[10240];
            int read;
            while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                ftpStream.Write(buffer, 0, read);
                uploadProgress.Invoke(
                    (MethodInvoker)delegate {
                        uploadProgress.Value = (int)fileStream.Position;

                        toolTip1.SetToolTip(
                            uploadProgress, string.Format("{0} MB's / {1} MB's\n",
                            (uploadProgress.Value / 1024d / 1024d).ToString("0.00"),
                            (fileStream.Length / 1024d / 1024d).ToString("0.00")));
                    });       
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Thanks

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Gerald Oakham
  • 173
  • 12

1 Answers1

1

Your code works for me. Assuming you run the uploadFile on a background thread, like:

private void button1_Click(object sender, EventArgs e)
{
    Task.Run(() => uploadFile());
}

enter image description here

See also How can we show progress bar for upload with FtpWebRequest
(though you know that link already)


You just update the tooltip too often, so it flickers.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks Martin, It tried the 'task.Run' line, and it works, ...but I had to remove any writes to a richtextbox I have , otherwise I get 'Cross-Thread operation not valid. Control 'richtextbox1' accessed from a thread other than the thread it was created on. is there anyway to resolve this ? – Gerald Oakham Sep 01 '17 at 08:24
  • The same way you access the `uploadProgress` and `toolTip1`. Use `Control.Invoke`. – Martin Prikryl Sep 01 '17 at 08:26
  • Don't worry, https://stackoverflow.com/questions/8738767/backgroundworker-cross-thread-operation-not-valid solves that issue for me. thanks again – Gerald Oakham Sep 01 '17 at 08:26