void processor_ProgressChanged(object sender, ProgressEventArgs e) { if (InvokeRequired) { BeginInvoke( new UpdateProgressBarCallback(UpdateProgressBar), new object[] { e.Completed, e.Total } ); } }
/// <summary> /// Loops over the given directory and passes each file to ProcessFile /// </summary> /// <param name="directory"></param> protected void ProcessDirectory(string directory) { if (Directory.Exists(directory)) { DirectoryInfo dirInfo = new DirectoryInfo(directory); FileInfo[] files = dirInfo.GetFiles(); List<FileInfo> imageFiles = new List<FileInfo>(); foreach (FileInfo fileInfo in files) { if (IsImageFile(fileInfo)) { imageFiles.Add(fileInfo); } } TotalFiles = imageFiles.Count; ProgressEventArgs args1 = new ProgressEventArgs(ProcessedFiles, TotalFiles); OnProgressChanged(args1); foreach (FileInfo fileInfo in imageFiles) { if (Canceled) { return; } ProcessFile(fileInfo); ProcessedFiles += 1; ProgressEventArgs args2 = new ProgressEventArgs(ProcessedFiles, TotalFiles); OnProgressChanged(args2); } } Completed = true; }
/// <summary> /// Raise the ProgressChanged event /// </summary> /// <param name="e"></param> protected virtual void OnProgressChanged(ProgressEventArgs e) { if (ProgressChanged != null) { ProgressChanged(this, e); } }