Changeset 14 for Common/FormProgress.cs
- Timestamp:
- Aug 2, 2022, 11:46:25 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/FormProgress.cs
r2 r14 2 2 using System.Collections.Generic; 3 3 using System.ComponentModel; 4 using System.Data;5 4 using System.Drawing; 6 5 using System.Linq; 7 using System.Text;8 6 using System.Windows.Forms; 9 7 … … 12 10 public partial class FormProgress : Form 13 11 { 14 public List<Progress> tasks;15 public DateTime startTime;16 public Progress currentTask;12 public List<Progress> Tasks; 13 public DateTime StartTime; 14 public Progress CurrentTask; 17 15 18 16 public FormProgress() … … 20 18 InitializeComponent(); 21 19 22 tasks = new List<Progress>();23 currentTask = null;20 Tasks = new List<Progress>(); 21 CurrentTask = null; 24 22 } 25 23 … … 27 25 { 28 26 int steps = 10000; 29 int total = tasks.Count * steps;30 int current = 0;31 int index = tasks.IndexOf(currentTask);27 int total = Tasks.Count * steps; 28 int current; 29 int index = Tasks.IndexOf(CurrentTask); 32 30 if (index >= 0) 33 31 { 34 32 current = index * steps; 35 if ( currentTask.total > 0) current += (int)(index + ((double)currentTask.current / currentTask.total) * steps);33 if (CurrentTask.Total > 0) current += (int)(index + ((double)CurrentTask.Current / CurrentTask.Total) * steps); 36 34 } 37 35 else current = 0; … … 40 38 else progressBar1.Value = total; 41 39 progressBar1.Visible = current > 0; 42 if ( currentTask != null) labelOperation.Text = currentTask.operation;40 if (CurrentTask != null) labelOperation.Text = CurrentTask.Operation; 43 41 else labelOperation.Text = ""; 44 42 if ((current > 0) && (total > 0)) 45 43 { 46 TimeSpan timeDiff = TimeSpan.FromSeconds((DateTime.Now - startTime).TotalSeconds / current * (total - current));44 TimeSpan timeDiff = TimeSpan.FromSeconds((DateTime.Now - StartTime).TotalSeconds / current * (total - current)); 47 45 string remaining = timeDiff.ToString(@"hh\:mm\:ss"); 48 if (timeDiff.TotalDays >= 1) remaining = Math.Truncate(timeDiff.TotalDays) .ToString()+ " days " + remaining;49 labelTime.Text = "Remaining time: " + remaining;46 if (timeDiff.TotalDays >= 1) remaining = Math.Truncate(timeDiff.TotalDays) + " days " + remaining; 47 labelTime.Text = @"Remaining time: " + remaining; 50 48 } 51 49 else labelTime.Text = ""; … … 56 54 Theme.UseTheme(this); 57 55 DpiScaling.Apply(this); 58 this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);56 Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); 59 57 60 58 timer1_Tick(this, null); … … 65 63 private void FormProgress_FormClosing(object sender, FormClosingEventArgs e) 66 64 { 67 currentTask.terminated = true;65 CurrentTask.Terminated = true; 68 66 timer1.Enabled = false; 69 67 } … … 71 69 private void bg_DoWork(object sender, DoWorkEventArgs e) 72 70 { 73 currentTask.Execute();74 currentTask.completed = !currentTask.terminated;71 CurrentTask.Execute(); 72 CurrentTask.Completed = !CurrentTask.Terminated; 75 73 } 76 74 … … 79 77 if (e.Error != null) 80 78 { 81 currentTask.terminated = true;82 MessageBox.Show(e.Error.Message, "Error in background task", MessageBoxButtons.OK, MessageBoxIcon.Error);79 CurrentTask.Terminated = true; 80 MessageBox.Show(e.Error.Message, @"Error in background task", MessageBoxButtons.OK, MessageBoxIcon.Error); 83 81 } 84 if (! currentTask.terminated)82 if (!CurrentTask.Terminated) 85 83 { 86 int nextIndex = tasks.IndexOf(currentTask) + 1;87 if (nextIndex < tasks.Count)84 int nextIndex = Tasks.IndexOf(CurrentTask) + 1; 85 if (nextIndex < Tasks.Count) 88 86 { 89 currentTask = tasks[nextIndex];87 CurrentTask = Tasks[nextIndex]; 90 88 BackgroundWorker bg = new BackgroundWorker(); 91 bg.DoWork += new DoWorkEventHandler(bg_DoWork);92 bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_DoWorkerCompleted);89 bg.DoWork += bg_DoWork; 90 bg.RunWorkerCompleted += bg_DoWorkerCompleted; 93 91 bg.RunWorkerAsync(); 94 92 } … … 100 98 public void AddTask(string name, Progress.CallbackEventHandler progressCallback) 101 99 { 102 Progress newTask = new Progress(); 103 newTask.terminated = false; 104 newTask.total = 0; 105 newTask.current = 0; 106 newTask.operation = name; 107 newTask.callback += progressCallback; 108 tasks.Add(newTask); 100 Progress newTask = new Progress 101 { 102 Terminated = false, 103 Total = 0, 104 Current = 0, 105 Operation = name 106 }; 107 newTask.Callback += progressCallback; 108 Tasks.Add(newTask); 109 109 } 110 110 111 111 public void StartTask(string name, Progress.CallbackEventHandler progressCallback) 112 112 { 113 tasks.Clear();113 Tasks.Clear(); 114 114 AddTask(name, progressCallback); 115 115 Start(); … … 118 118 public void Start() 119 119 { 120 if ( tasks.Count > 0)120 if (Tasks.Count > 0) 121 121 { 122 startTime = DateTime.Now;123 currentTask = tasks.First();122 StartTime = DateTime.Now; 123 CurrentTask = Tasks.First(); 124 124 BackgroundWorker bg = new BackgroundWorker(); 125 bg.DoWork += new DoWorkEventHandler(bg_DoWork);126 bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_DoWorkerCompleted);125 bg.DoWork += bg_DoWork; 126 bg.RunWorkerCompleted += bg_DoWorkerCompleted; 127 127 bg.RunWorkerAsync(); 128 128 ShowDialog(); … … 133 133 { 134 134 int completed = 0; 135 foreach (var task in tasks)135 foreach (var task in Tasks) 136 136 { 137 if (task. completed) completed += 1;137 if (task.Completed) completed += 1; 138 138 } 139 return completed == tasks.Count;139 return completed == Tasks.Count; 140 140 } 141 141 } … … 143 143 public interface IProgress 144 144 { 145 void SetTotal(int Value);146 void SetProgress(int Value);145 void SetTotal(int value); 146 void SetProgress(int value); 147 147 bool GetTerminated(); 148 148 void SetOperation(string text); … … 152 152 public class Progress : IProgress 153 153 { 154 public int total;155 public int current;156 public bool terminated;157 public bool completed; // If task was not terminated during execution.158 public string operation;154 public int Total; 155 public int Current; 156 public bool Terminated; 157 public bool Completed; // If task was not terminated during execution. 158 public string Operation; 159 159 public delegate void CallbackEventHandler(Progress progress); 160 public event CallbackEventHandler callback; 161 public Exception exception; 160 public event CallbackEventHandler Callback; 162 161 163 public void SetTotal(int Value)162 public void SetTotal(int value) 164 163 { 165 total = Value;164 Total = value; 166 165 } 167 166 168 public void SetProgress(int Value)167 public void SetProgress(int value) 169 168 { 170 current = Value;169 Current = value; 171 170 } 172 171 173 172 public bool GetTerminated() 174 173 { 175 return terminated;174 return Terminated; 176 175 } 177 176 178 177 public void SetOperation(string text) 179 178 { 180 operation = text;179 Operation = text; 181 180 } 182 181 183 182 public void Execute() 184 183 { 185 if (callback != null) callback(this);184 Callback?.Invoke(this); 186 185 } 187 186 }
Note:
See TracChangeset
for help on using the changeset viewer.