-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form2.cs
105 lines (93 loc) · 3.39 KB
/
Form2.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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Timers;
using System.Windows.Forms;
namespace CoreTracker
{
public partial class Form2 : Form
{
Stopwatch sw = new Stopwatch();
int autoCloseCount = 0;
int autoCloseCountMAX = 5;
static bool downloadSuccessful = false;
public System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form2()
{
InitializeComponent();
}
// The event that will fire whenever the progress of the WebClient is changed
public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
// Calculate download speed and output it to labelSpeed.
downloadLabelSet(string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00")));
// Update the progressbar percentage only when the value is not the same.
downloadBarValueSet(e.ProgressPercentage);
// Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
downloadLabelSet(string.Format("{0} MB's / {1} MB's / {2}",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"),
e.ProgressPercentage.ToString() + "%"
));
Application.DoEvents();
}
public bool getDownloadStatus()
{
return downloadSuccessful;
}
private void downloadCompleted(Object source, EventArgs e)
{
Application.DoEvents();
if (autoCloseCount >= autoCloseCountMAX)
{
downloadSuccessful = true;
timer.Stop();
timer.Dispose();
} else
{
lb_download_status.Text = $"Download completed!, Auto start new installer {autoCloseCountMAX - autoCloseCount} sec later";
autoCloseCount++;
}
}
// The event that will trigger when the WebClient is completed
public void Completed(object sender, AsyncCompletedEventArgs e)
{
// Reset the stopwatch.
sw.Reset();
timer.Interval = 1000;
timer.Tick += new EventHandler(downloadCompleted);
timer.Stop();
autoCloseCount = 0;
timer.Start();
}
#region CrossThread
delegate void lb_download_status_set(string data);
delegate void lb_download_status_value(int data);
private void downloadLabelSet(string data)
{
if (lb_download_status.InvokeRequired)
{
lb_download_status_set call = new lb_download_status_set(downloadLabelSet);
this.Invoke(call, data);
}
else
{
lb_download_status.Text = data;
}
}
private void downloadBarValueSet(int data)
{
if (lb_download_status.InvokeRequired)
{
lb_download_status_value call = new lb_download_status_value(downloadBarValueSet);
this.Invoke(call, data);
}
else
{
pb_download.Value = data;
}
}
#endregion
}
}