// local thread only: private void _purge() { if (This != null && m_closeWhenDone && ProgressMonitor.List.Count == 0 && formUpDateTime.AddSeconds(SECONDS_TO_STAY_UP).CompareTo(DateTime.Now) < 0) { This.Close(); return; } int deleted = 0; try { if (!keepFinishedCheckBox.Checked) { for (int i = List.Count - 1; i >= 0; i--) { DisplayElement de = (DisplayElement)List.GetByIndex(i); if (de.monitored.Purged) { this.Controls.Remove(de.progressBar); this.Controls.Remove(de.label); List.RemoveAt(i); deleted++; } } } } catch {} finally { if (deleted > 0) { RebuildGui(); } } }
// local thread only: private void _WorkValues() { try { for (int i = 0; i < List.Count; i++) { DisplayElement de = (DisplayElement)List.GetByIndex(i); de.progressBar.Value = de.monitored.Progress > 100 ? 100 : de.monitored.Progress; de.label.Text = "" + de.monitored; if (de.monitored.Complete) { if (de.monitored.Success) { de.label.ForeColor = Color.Green; } else { de.label.ForeColor = Color.Red; } } } } catch {} }
// must be called from this form's thread - actually is called from the constructor and from Paint() private void RebuildProgressBars() { inRebuild = true; this.SuspendLayout(); try { int i; for (i = 0; i < List.Count; i++) { DisplayElement de = (DisplayElement)List.GetByIndex(i); this.Controls.Remove(de.progressBar); this.Controls.Remove(de.label); } List.Clear(); int yPos = 48; int activeCount = 0; for (i = 0; i < ProgressMonitor.List.Count && yPos < 1000; i++) { DateTime key = (DateTime)ProgressMonitor.List.GetKey(i); Monitored mm = (Monitored)ProgressMonitor.List.GetByIndex(i); DisplayElement de = new DisplayElement(); de.monitored = mm; if (!mm.Complete) { activeCount++; } // // progressBar // de.progressBar.Location = new System.Drawing.Point(16, yPos); de.progressBar.Name = "progressBar" + i; de.progressBar.Size = new System.Drawing.Size(120, 16); de.progressBar.TabIndex = 1; // // progressLabel // de.label.Location = new System.Drawing.Point(152, yPos); de.label.Name = "progressLabel" + i; de.label.Size = new System.Drawing.Size(800, 16); de.label.TabIndex = 2; List.Add(key, de); this.Controls.Add(de.progressBar); this.Controls.Add(de.label); yPos += 24; } int newHeight = yPos + 20; // do not allow it to resize too often, just when it grows or shrinks fast. if (ProgressMonitor.List.Count == 0 || newHeight > this.ClientSize.Height || newHeight < this.ClientSize.Height - 120) { int width = this.ClientSize.Width; this.ClientSize = new System.Drawing.Size(width, newHeight); } headLabel.Text = "Download Tasks - total:" + i + " active: " + activeCount + " threads: " + ThreadPool2.ActiveThreads //Project.threadPool.CurrentThreadCount + " queue: " + ThreadPool2.WaitingCallbacks; //Project.threadPool.RequestQueueCount; _WorkValues(); // set all current values } catch {} finally { this.ResumeLayout(false); inRebuild = false; } }