report progress
Is there anyway to report progress to a background worker with session.GetFiles?
Thank you.
Thank you.
Advertisement
Advertisement
FileStream stream = File.OpenRead(filePath); byte[] buffer = new byte[2048]; int contentLen = stream.Read(buffer, 0, buffer.Length); int totalReadBytesCount = contentLen; Stream reqStream = request.GetRequestStream(); while (contentLen != 0) { reqStream.Write(buffer, 0, buffer.Length); contentLen = stream.Read(buffer, 0, buffer.Length); totalReadBytesCount += contentLen; var progress = totalReadBytesCount * 100.0 / stream.Length; bw.ReportProgress((int)progress); }
public static string FtpFilesSecurely(string file, string host, string direction, BackgroundWorker bw) { var fileInfo = session.GetFileInfo(file); //CalculateDownload worker1.DoWork += (sender, e) => e.Result = worker1_DoWork(fileInfo, FTP, bw, file); worker1.RunWorkerAsync(); //Download files TransferOperationResult transferResult = session.GetFiles(file, FTP.sourceDirectory, false, transferOptions); // Throw on any error transferResult.Check(); // Print results return transferResult.IsSuccess ? "Download Succeeded." : "Download Failed."; } public static object worker1_DoWork(RemoteFileInfo fileInfo, CompanyFtpInfo FTP, BackgroundWorker bw, string file) { const string filePart = ".filepart"; file = FTP.sourceDirectory + file + filePart; while (!File.Exists(file)) { System.Threading.Thread.Sleep(1000); } CalculateDownload(fileInfo, bw, file); return "Calculation Complete."; } private static void CalculateDownload(RemoteFileInfo fileInfo, BackgroundWorker bw, string file) { long remoteFileSize = fileInfo.Length; var localFile = new FileInfo(file); long localFileSize = localFile.Length; while (localFileSize < remoteFileSize) { long progress = localFileSize*100/remoteFileSize; bw.ReportProgress((int) progress); localFile = new FileInfo(file); localFileSize = localFile.Length; //this works because as a file is downloaded it is called filename.ext.filepart //when the file is done downloading it is then called filename.ext so therefore //the original name doesnt exist anymore. if (!File.Exists(file)) break; } }
Advertisement
You can post new topics in this forum