public void SetProgress(long inSize, long outSize) { // Called every 100 milliseconds int permille = (int)(Math.Round(1000.0 * inSize / totalLogSize)); if (permille > 1000) { permille = 1000; } CompressWorker.ReportProgress(permille, new ProgressInfo { CompressedSize = outSize }); }
private void CompressWorker_DoWork(object sender, DoWorkEventArgs args) { try { // Quickly set the UI state CompressWorker.ReportProgress(0, new ProgressInfo { CompressedSize = 0 }); // Collect all data DateTime minTime = DateTime.MinValue; if (SharedData.Instance.LogTimeSpan != TimeSpan.MaxValue) { minTime = SharedData.Instance.LastLogUpdateTime - SharedData.Instance.LogTimeSpan; } string notes = SharedData.Instance.Notes; string eMailAddress = SharedData.Instance.EMailAddress; List <string> logFiles = new List <string>(); totalLogSize = 0; long processedLogSize = 0; foreach (string basePath in SharedData.Instance.LogBasePaths) { string dir = Path.GetDirectoryName(basePath); string baseName = Path.GetFileName(basePath); var interestingFiles = Directory.GetFiles(dir, baseName + "-?-*.fl") .Concat(Directory.GetFiles(dir, baseName + "-scr-*.png")) .Concat(Directory.GetFiles(dir, baseName + "-scr-*.jpg")); foreach (string logFile in interestingFiles) { FileInfo fi = new FileInfo(logFile); if (fi.LastWriteTimeUtc >= minTime) { logFiles.Add(logFile); totalLogSize += fi.Length; } } } if (logFiles.Count == 0) { throw new Exception(Tx.T("msg.no log files selected")); } InitializeArchive(); if (!string.IsNullOrWhiteSpace(notes)) { using (var notesStream = new MemoryStream()) using (var writer = new StreamWriter(notesStream, Encoding.UTF8)) { writer.Write(notes); writer.Flush(); notesStream.Seek(0, SeekOrigin.Begin); AddFile(notesStream, "notes.txt", DateTime.UtcNow); } } if (!string.IsNullOrWhiteSpace(eMailAddress)) { using (var eMailStream = new MemoryStream()) using (var writer = new StreamWriter(eMailStream, Encoding.UTF8)) { writer.Write("[InternetShortcut]\r\nURL=mailto:"); writer.Write(eMailAddress); writer.Flush(); eMailStream.Seek(0, SeekOrigin.Begin); AddFile(eMailStream, "email.url", DateTime.UtcNow); } } EnsureDirectory("log/"); foreach (string logFile in logFiles) { if (CompressWorker.CancellationPending) { args.Cancel = true; return; } FileInfo fi = new FileInfo(logFile); AddFile(logFile, "log/" + Path.GetFileName(logFile)); processedLogSize += fi.Length; //int percent = (int) (Math.Round(100.0 * processedLogSize / totalLogSize)); //CompressWorker.ReportProgress(percent, new ProgressInfo { CompressedSize = gzFile.Length }); } if (CompressWorker.CancellationPending) { args.Cancel = true; return; } CloseArchive(); CompressArchive(); FileInfo archiveInfo = new FileInfo(archiveFileName2); CompressWorker.ReportProgress(1000, new ProgressInfo { CompressedSize = archiveInfo.Length }); } catch (ObjectDisposedException) { // We're leaving, don't do anything more. // Somehow this isn't caught by the BackgroundWorker management. return; } }