Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using the following code to tie up some eventhandlers to the client.UploadProgressChanged and client.UploadFileCompletedEventHandler
C#
try
{
    WebClient client = new WebClient();
    client.Credentials = new NetworkCredential(txtFTPUserName.Text, txtFTPPassword.Text);
    client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressChangedEventArgs);
    client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCompletedEventHandler);
    progBar.Visible = true;
    progBar.Value = 0;
    client.UploadFile(@"ftp://novantconsulting.com/SQLBackups/StrataFrame.bak", @"E:\Strataframe.bak");
 }
catch (Exception exception)
{
    Console.WriteLine(exception);
    throw;
}

Here is the event handler:
C#
private void UploadProgressChangedEventArgs(object sender, UploadProgressChangedEventArgs e)
{
    int percentage = (Convert.ToInt32(e.BytesSent) / Convert.ToInt32(e.TotalBytesToSend)) * 100;

    if (progBar.Value < progBar.Maximum)
    {
        progBar.Value = percentage;
    }
    else
    {
        progBar.Value = 100;
    }
}


Obviously I'm trying to display a status bar but the event never triggers.

Thanks for your help in advance,

CTBlankenship

What I have tried:

Just what you see in the code ... I Googled the dickens out of it and am starting to think that an async solution is coming my way. I just haven't done any async work yet.
Posted
Updated 2-Nov-19 0:04am
Comments
RickZeeland 2-Nov-19 5:55am    
Are you using WPF ? that's a known problem in WPF.
Charles T. Blankenship 2-Nov-19 12:19pm    
No ... WinForms ...

1 solution

You probably need to use Invoke, as the progressbar is not set from the main UI thread, see article: Another Way to Invoke UI from a Worker Thread[^]
You may also need a Application.DoEvents() to show the updates to the control.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900