-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgressBar.cs
107 lines (93 loc) · 5.49 KB
/
ProgressBar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DeltaZip
{
public partial class ProgressBar : Form
{
MethodInvoker onTick;
MethodInvoker onCancel;
public ProgressBar()
{
InitializeComponent();
timer1.Tick += delegate { onTick(); };
buttonCancel.Click += delegate { onCancel(); };
}
public void Bind(ArchiveWriter.Stats stats)
{
onTick = delegate {
this.Text = stats.Title;
progressBar1.Value = Math.Max(0, Math.Min(100, (int)(stats.Progress * 100)));
textStatus.Text = stats.Status;
long total = stats.Compressed + stats.SavedByCompression + stats.SavedByInternalDelta + stats.SavedByExternalDelta;
TimeSpan time = (stats.EndTime ?? DateTime.Now) - stats.StartTime;
labelL1.Text = "Total processed:"; textL1.Text = FormatSize(total);
labelL2.Text = "Elapsed time:"; textL2.Text = string.Format("{0}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
labelL3.Text = "Block size:"; textL3.Text = FormatSize(Settings.BlockSize);
labelL4.Text = ""; textL4.Text = "";
labelR1.Text = "Compressed size:"; textR1.Text = FormatSize(stats.Compressed, total);
labelR2.Text = "Saved by compression:"; textR2.Text = FormatSize(stats.SavedByCompression, total);
labelR3.Text = "Saved by internal delta:"; textR3.Text = FormatSize(stats.SavedByInternalDelta, total);
labelR4.Text = "Saved by external delta:"; textR4.Text = FormatSize(stats.SavedByExternalDelta, total);
buttonCancel.Enabled = (stats.EndTime == null);
};
onCancel = delegate {
stats.Canceled = true;
stats.EndTime = DateTime.Now;
};
}
public void Bind(ArchiveReader.Stats stats)
{
onTick = delegate {
this.Text = stats.Title;
progressBar1.Value = Math.Max(0, Math.Min(100, (int)(stats.Progress * 100)));
textStatus.Text = stats.Status;
TimeSpan time = (stats.EndTime ?? DateTime.Now) - stats.StartTime;
TimeSpan writeTime = (stats.EndTime ?? DateTime.Now) - (stats.WriteStartTime ?? DateTime.Now);
long writeSpeed = (int)writeTime.TotalSeconds == 0 ? 0 : stats.TotalWritten / (int)writeTime.TotalSeconds;
labelL1.Text = "Total processed:"; textL1.Text = FormatSize(stats.TotalWritten + stats.Unmodified);
labelL2.Text = "Elapsed time:"; textL2.Text = string.Format("{0}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
labelL3.Text = "Average speed:"; textL3.Text = writeSpeed == 0 ? "-" : (FormatSize(writeSpeed) + "/s");
labelL4.Text = ""; textL4.Text = "";
labelR1.Text = "Unmodified:"; textR1.Text = FormatSize(stats.Unmodified, stats.Unmodified + stats.ReadFromArchiveCompressed + stats.ReadFromWorkingCopy);
labelR2.Text = "Read from archive:"; textR2.Text = FormatSize(stats.ReadFromArchiveCompressed, stats.Unmodified + stats.ReadFromArchiveCompressed + stats.ReadFromWorkingCopy);
labelR3.Text = "Read from working copy:"; textR3.Text = FormatSize(stats.ReadFromWorkingCopy, stats.Unmodified + stats.ReadFromArchiveCompressed + stats.ReadFromWorkingCopy);
labelR4.Text = ""; textR4.Text = "";
buttonCancel.Enabled = (stats.EndTime == null);
};
onCancel = delegate {
stats.Canceled = true;
stats.EndTime = DateTime.Now;
};
}
static string FormatSize(long size, long outOf)
{
return FormatSize(size) + string.Format(" ({0}%)", outOf == 0 ? 0 : 100 * size / outOf);
}
static string FormatSize(long size)
{
if (size >= (long)10 * 1024 * 1024 * 1024) return (size / (1024 * 1024 * 1024)) .ToString() + " GiB";
if (size >= 1024 * 1024 * 1024) return ((float)size / (1024 * 1024 * 1024)) .ToString("F1") + " GiB";
if (size >= 10 * 1024 * 1024) return (size / (1024 * 1024)) .ToString() + " MiB";
if (size >= 1024 * 1024) return ((float)size / (1024 * 1024)) .ToString("F1") + " MiB";
if (size >= 10 * 1024) return (size / (1024)) .ToString() + " KiB";
if (size >= 1024) return ((float)size / (1024)) .ToString("F1") + " KiB";
return size.ToString() + " B";
}
private void ProgressBar_FormClosing(object sender, FormClosingEventArgs e)
{
buttonCancel.PerformClick();
}
private void ProgressBar_FormClosed(object sender, FormClosedEventArgs e)
{
}
public new void Close()
{
this.BeginInvoke((MethodInvoker)delegate { base.Close(); });
}
}
}