0

i am trying to upload on ftp, using UploadFileAsync method, i need to update a progress bar this is why i maked it asynchronously but i also want to wait until the upload complets then i continu processing there is what i am doing , but it want works, i tried also with Task.RunSynchronously() ... for Uploading :

private async Task<Hashtable>  UploadFile(FileSended file , string folder)
    {
        bool isUploaded = true;
        string fileName = file.FileName;
        string msg = "";
        data = new Hashtable();
        try
        {             
            completed = false;

            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential(UserId, Password);
                Uri siteUri = new Uri(Host + "/" + folder + "/" + fileName);

                client.UploadFileCompleted += WebClientUploadCompleted;
                client.UploadProgressChanged += WebClientUploadProgressChanged;
       
                await Task.Run(() => UploadToFtp(client, siteUri,file.LocalPath)) ; 
            }

            for (int i = 0; i < ProgressFtp.Count; i++)
            {
                parentForm.progressBar.Value = ProgressFtp[i];
                parentForm.lbStatus.Text = "Progress : " + parentForm.progressBar.Value + "%";
                parentForm.lbStatus.Refresh();  
            }
            msg = "OK";
        }
        catch (Exception e)
        {
            isUploaded = false;
        }
 
        data.Add("IsUploaded", isUploaded);
        data.Add("Message", msg);

        return data;
    }

the method :

 private  static bool UploadToFtp(WebClient client, Uri uri, string localPath)
    {
        bool uploaded = true;

        try
        {
            client.UploadFileAsync(uri, WebRequestMethods.Ftp.UploadFile, file.LocalPath);
        }
        catch (Exception e)
        {
            Message = e.Message;
            uploaded = false;
        }
        return uploaded;
    }

WebClientUploadProgressChanged :

private static void WebClientUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
    {
        ProgressFtp.Add(e.ProgressPercentage);
    }

ProgressFTP is a int list that i use to update the progressBar , because when i do it on WebClientUploadProgressChanged , it cause an exception :

cross thread operation not valid

so i gone around it .

Yamine Klioui
  • 55
  • 2
  • 9
  • What is `ProgressFtp`? – Ackdari Mar 29 '21 at 12:15
  • it is a list of int that contains the progress reported by e.ProgressChanged – Yamine Klioui Mar 29 '21 at 12:30
  • private static void WebClientUploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { ProgressFtp.Add(e.ProgressPercentage); } – Yamine Klioui Mar 29 '21 at 12:31
  • the problem is not on uploading and updating the progress bar, i did it well , but i want when it happens, the program wait for uploading and wait for the progress bar until 100% then continu other instructions, in my code , the program continu without waiting the upload to be done, that is not what i want – Yamine Klioui Mar 29 '21 at 12:34
  • What do you mean by *"program wait"*? It's GUI, it cannot stop. It must run, otherwise the GUI will freeze. – Martin Prikryl Mar 29 '21 at 13:15
  • You may want to take a look at the [`Progress`](https://learn.microsoft.com/en-us/dotnet/api/system.progress-1) class, and [how it's used](https://blog.stephencleary.com/2012/02/reporting-progress-from-async-tasks.html). Another helpful tutorial is [here](https://devblogs.microsoft.com/dotnet/async-in-4-5-enabling-progress-and-cancellation-in-async-apis/). – Theodor Zoulias Mar 29 '21 at 19:36

1 Answers1

0

The method UploadFileAsync is an older style background thread upload. It does not block. So, invoking it in a Task.Run will let the Task finis immediately. If it would work the way you'd expected, the for loop after the await wouldn't be executed while the upload is being processed.

So, I'd recommend using the UploadFileTaskAsync method without starting a custom Task. This way, the events should also fire on the main thread, I believe, and you wouldn't need the list.

Rezun
  • 93
  • 3