0

I'm noobie in C# so, please don't be cruel with me if I'm wrong.

I'm trying to do a progress bar, but I can't achieve the progress bar works.

This is my code and it's closer of this example Displaying progress of file download in a ProgressBar with SSH.NET but in my case it don't use "Task.Run(() => Download());" because I need to wait until download will be completed.

What I'm doing wrong? or how can achieve?

why "progressbar1" doesn't update the field "value"?

Is it weird because when I did a debug I can see how it stops on this line:

"progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = (int)bytesRead; });"

on DownloadProgresBar but it doesn't update the progressbar but on the other hand it works this one "Console.WriteLine("Downloaded:" + bytesRead);"

Below my code:

private void Download(string updateDataLoc, int updateDataLocPort, string updatUsr, string updatPas, string RemPath, string LocPath, string fichero)
    {
        try
        {
            int Port = updateDataLocPort;
            string Host = updateDataLoc;
            string Username = updatUsr;
            string Password = updatPas;
            string RemotePath = RemPath; // "/remote/path/";
            string SourcePath = LocPath; // @"C:\local\path\";
            string FileName = fichero; // "download.txt";

            
            string SourceFilePath = SourcePath; //includes the file name
            using (var stream = new FileStream(SourcePath, FileMode.Create))
            using (var client = new SftpClient(Host, Port, Username, Password))
            {
                client.Connect();
                SftpFileAttributes attributes = client.GetAttributes(RemotePath + FileName);
                // Set progress bar maximum on foreground thread
                progressBar1.Invoke(
                    (MethodInvoker)delegate { progressBar1.Maximum = (int)attributes.Size; });
                // Download with progress callback
                client.DownloadFile(RemotePath + FileName, stream, downloadCallback: DownloadProgresBar);
                MessageBox.Show("Download complete");
            }

        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
    private void DownloadProgresBar(ulong bytesRead)
    {
        Console.WriteLine("Downloaded:" + bytesRead);
        // Update progress bar on foreground thread
        progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = (int)bytesRead; });
    }
sigsag
  • 37
  • 8
  • Are you running this on the UI thread or on the a different thread? – Charlieface Mar 23 '23 at 00:34
  • *"I need to wait"* => What does that mean exactly in your GUI application? Do you want the user to prevent doing anything with the window until the download completes? Or what? – Martin Prikryl Mar 23 '23 at 06:38
  • "Are you running this on the UI thread or on the a different thread?" I'm running on the UI thread. I'm showing a splash screen and I want to show in this splash screen a message informing to the user there is a big file is downloading and a progress bar with the progress. I need to wait on this splash until the download finish but meanwhile show how increase the progress bar. – sigsag Mar 23 '23 at 07:36
  • Sorry for my English, I try to explain my best.|| "I need to wait" => What does that mean exactly in your GUI application? I'm showing a splash screen and I would like show this screen until the download is complete. This works, but when I try to show a progress bar this not increase. || Do you want the user to prevent doing anything with the window until the download completes? yes, I want the user stay there (splash screen) until finish the download file. I hope I could answer the questions with the aim you help me. Thanks. – sigsag Mar 23 '23 at 07:42
  • All you need to do is to use the code from https://stackoverflow.com/q/44442714/850848 – And just make sure that during the download, the user cannot do anything else with the GUI (disable all controls and prevent form close). – Martin Prikryl Mar 23 '23 at 08:58
  • Yes, I've been reviewing this post (you suggested to me) but if I add: "Task.Run(() => Download());" the program continues running to next sentence (obvious we're creating a thread) but I don't want to continue. Sorry, I'm missing something. :(( – sigsag Mar 23 '23 at 09:19
  • how can I disable all controls? or what does mean this suggestion? If I undestand I should control the rest of the code until the process download finish, is that right? – sigsag Mar 23 '23 at 09:21
  • You CANNOT stop the main thread in an GUI application. You have to change your paradigm. To disable a UI control use [`Control.Enabled` property](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.enabled). – Martin Prikryl Mar 23 '23 at 09:53
  • Ok, let me check and I'll let you know. – sigsag Mar 23 '23 at 10:23
  • I'm lost, please can you give to me some guidelines or some code. thx. – sigsag Mar 23 '23 at 21:04
  • Show us what did you try based on my suggestions and we can try to help you from there. Because to be honest, I cannot see what problem you might have. The https://stackoverflow.com/q/44442714/850848 has basically all you need. – Martin Prikryl Mar 24 '23 at 06:16

0 Answers0