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; });
}