Changeset 14 for Common/FormProgress.cs


Ignore:
Timestamp:
Aug 2, 2022, 11:46:25 AM (22 months ago)
Author:
chronos
Message:
  • Modified: Various improvements.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/FormProgress.cs

    r2 r14  
    22using System.Collections.Generic;
    33using System.ComponentModel;
    4 using System.Data;
    54using System.Drawing;
    65using System.Linq;
    7 using System.Text;
    86using System.Windows.Forms;
    97
     
    1210    public partial class FormProgress : Form
    1311    {
    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;
    1715
    1816        public FormProgress()
     
    2018            InitializeComponent();
    2119
    22             tasks = new List<Progress>();
    23             currentTask = null;
     20            Tasks = new List<Progress>();
     21            CurrentTask = null;
    2422        }
    2523
     
    2725        {
    2826            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);
    3230            if (index >= 0)
    3331            {
    3432                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);
    3634            }
    3735            else current = 0;
     
    4038                else progressBar1.Value = total;
    4139            progressBar1.Visible = current > 0;
    42             if (currentTask != null) labelOperation.Text = currentTask.operation;
     40            if (CurrentTask != null) labelOperation.Text = CurrentTask.Operation;
    4341                else labelOperation.Text = "";
    4442            if ((current > 0) && (total > 0))
    4543            {
    46                 TimeSpan timeDiff = TimeSpan.FromSeconds((DateTime.Now - startTime).TotalSeconds / current * (total - current));
     44                TimeSpan timeDiff = TimeSpan.FromSeconds((DateTime.Now - StartTime).TotalSeconds / current * (total - current));
    4745                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;
    5048            }
    5149            else labelTime.Text = "";
     
    5654            Theme.UseTheme(this);
    5755            DpiScaling.Apply(this);
    58             this.Icon = Icon.ExtractAssociatedIcon(Application.Execut‌​ablePath);
     56            Icon = Icon.ExtractAssociatedIcon(Application.Execut‌​ablePath);
    5957
    6058            timer1_Tick(this, null);
     
    6563        private void FormProgress_FormClosing(object sender, FormClosingEventArgs e)
    6664        {
    67             currentTask.terminated = true;
     65            CurrentTask.Terminated = true;
    6866            timer1.Enabled = false;
    6967        }
     
    7169        private void bg_DoWork(object sender, DoWorkEventArgs e)
    7270        {
    73             currentTask.Execute();
    74             currentTask.completed = !currentTask.terminated;
     71            CurrentTask.Execute();
     72            CurrentTask.Completed = !CurrentTask.Terminated;
    7573        }
    7674
     
    7977            if (e.Error != null)
    8078            {
    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);
    8381            }
    84             if (!currentTask.terminated)
     82            if (!CurrentTask.Terminated)
    8583            {
    86                 int nextIndex = tasks.IndexOf(currentTask) + 1;
    87                 if (nextIndex < tasks.Count)
     84                int nextIndex = Tasks.IndexOf(CurrentTask) + 1;
     85                if (nextIndex < Tasks.Count)
    8886                {
    89                     currentTask = tasks[nextIndex];
     87                    CurrentTask = Tasks[nextIndex];
    9088                    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;
    9391                    bg.RunWorkerAsync();
    9492                }
     
    10098        public void AddTask(string name, Progress.CallbackEventHandler progressCallback)
    10199        {
    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);
    109109        }
    110110
    111111        public void StartTask(string name, Progress.CallbackEventHandler progressCallback)
    112112        {
    113             tasks.Clear();
     113            Tasks.Clear();
    114114            AddTask(name, progressCallback);
    115115            Start();
     
    118118        public void Start()
    119119        {
    120             if (tasks.Count > 0)
     120            if (Tasks.Count > 0)
    121121            {
    122                 startTime = DateTime.Now;
    123                 currentTask = tasks.First();
     122                StartTime = DateTime.Now;
     123                CurrentTask = Tasks.First();
    124124                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;
    127127                bg.RunWorkerAsync();
    128128                ShowDialog();
     
    133133        {
    134134            int completed = 0;
    135             foreach (var task in tasks)
     135            foreach (var task in Tasks)
    136136            {
    137                 if (task.completed) completed += 1;
     137                if (task.Completed) completed += 1;
    138138            }
    139             return completed == tasks.Count;
     139            return completed == Tasks.Count;
    140140        }
    141141    }
     
    143143    public interface IProgress
    144144    {
    145         void SetTotal(int Value);
    146         void SetProgress(int Value);
     145        void SetTotal(int value);
     146        void SetProgress(int value);
    147147        bool GetTerminated();
    148148        void SetOperation(string text);
     
    152152    public class Progress : IProgress
    153153    {
    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;
    159159        public delegate void CallbackEventHandler(Progress progress);
    160         public event CallbackEventHandler callback;
    161         public Exception exception;
     160        public event CallbackEventHandler Callback;
    162161
    163         public void SetTotal(int Value)
     162        public void SetTotal(int value)
    164163        {
    165             total = Value;
     164            Total = value;
    166165        }
    167166
    168         public void SetProgress(int Value)
     167        public void SetProgress(int value)
    169168        {
    170             current = Value;
     169            Current = value;
    171170        }
    172171
    173172        public bool GetTerminated()
    174173        {
    175             return terminated;
     174            return Terminated;
    176175        }
    177176
    178177        public void SetOperation(string text)
    179178        {
    180             operation = text;
     179            Operation = text;
    181180        }
    182181
    183182        public void Execute()
    184183        {
    185             if (callback != null) callback(this);
     184            Callback?.Invoke(this);
    186185        }
    187186    }
Note: See TracChangeset for help on using the changeset viewer.