private void encodingBackgroundWorker2_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; EncoderParams args = (EncoderParams)e.Argument; EncoderResults results = new EncoderResults("", "", args.JobQueue.Count); bw.ReportProgress(0, results); //encodeStatus.Text = "Setting up threads..."; // set up encoder Encoder encoderManager = new Encoder(args, bw); List <Thread> threadList = new List <Thread>(); // set up 'n' threads for processing the queue for (int i = 0; i < args.Threads; i++) { Thread encoderThread = new Thread( new ThreadStart(encoderManager.encoderThread)); encoderThread.IsBackground = true; threadList.Add(encoderThread); // Start the thread. encoderThread.Start(); } for (int i = 0; i < args.Threads; i++) { threadList[i].Join(); } }
private void encodingBackgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e) { EncoderResults results = (EncoderResults)e.UserState; int queuesize = results.QueueCount; if (!String.IsNullOrEmpty(results.ConsoleText)) { consoleMessagesQueued = true; lock (lockObject) { resultsList.Add(results); } } // need to account for files the other threads are currently encoding int otherThreads = (int)threadCounter.Value - 1; int maximum = encodeProgress.Maximum; if (encodeAborted) { queuesize = abortedQueueSize; } int progress = maximum - (queuesize + otherThreads); encodeProgress.Style = ProgressBarStyle.Continuous; // max() included so that the progress never goes backwards progress = Math.Max(encodeProgress.Value, progress); // min() included for bounds checking encodeProgress.Value = Math.Min((progress), encodeProgress.Maximum); // update the status text encodeStatus.Text = "" + encodeProgress.Value + " of " + encodeProgress.Maximum + " files completed"; }
public void encoderThread() { FileInfo fi = null; while (jobQueue.Count > 0) { try { lock (lockObject) { fi = jobQueue.Dequeue(); } } // this type of exception will occur when queue is empty on dequeue -- just in case the lock // isn't sufficient, we don't want that condition to be fatal catch (InvalidOperationException) { } String fullName = ""; String consoleText = ""; try { fullName = fi.FullName; } catch (PathTooLongException) { consoleText = "The full path for this file is too long for FlacSquisher to handle."; } if (String.IsNullOrEmpty(consoleText)) { consoleText = encodeFile(fi); } else { fullName = fi.Name; } EncoderResults results = new EncoderResults(String.Copy(fullName), String.Copy(consoleText), jobQueue.Count); // increment "value" on the progress bar by one bw.ReportProgress(20, results); } }