Skip to content

Commit c5f6638

Browse files
Added progress update, downloaded kb so far is displayed every 3 seconds
1 parent a0d2991 commit c5f6638

File tree

1 file changed

+70
-14
lines changed

1 file changed

+70
-14
lines changed

Source/Menu/DownloadContentForm.cs

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
using GNU.Gettext;
2424
using LibGit2Sharp;
2525
using ORTS.Settings;
26-
using Newtonsoft.Json;
2726
using System.Linq;
28-
using System.Security.Policy;
27+
using System.Diagnostics;
28+
using System.Threading;
2929

3030
namespace ORTS
3131
{
@@ -79,7 +79,7 @@ void dataGridViewDownloadContent_SelectionChanged(object sender, EventArgs e)
7979

8080
private void InstallPathButton_Click(object sender, EventArgs e)
8181
{
82-
using (var folderBrowser = new FolderBrowserDialog())
82+
using (FolderBrowserDialog folderBrowser = new FolderBrowserDialog())
8383
{
8484
folderBrowser.SelectedPath = InstallPathTextBox.Text;
8585
folderBrowser.Description = "Main Path where route is to be installed";
@@ -154,19 +154,13 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
154154
Cursor.Current = Cursors.WaitCursor;
155155

156156
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = Catalog.GetString("Installing...");
157-
this.Refresh();
157+
Refresh();
158158

159-
try
160-
{
161-
Repository.Clone(Routes[RouteName].Url, installPathRoute);
162-
}
163-
catch (LibGit2SharpException libGit2SharpException)
159+
// actual download
160+
161+
if (!downloadRoute(installPathRoute))
164162
{
165-
{
166-
message = Catalog.GetStringFmt("Error during download: {0}", libGit2SharpException.Message);
167-
MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OK, MessageBoxIcon.Error);
168-
return;
169-
}
163+
return;
170164
}
171165

172166
// insert row in Options, tab Content
@@ -228,5 +222,67 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
228222

229223
Close();
230224
}
225+
226+
private bool downloadRoute(string installPathRoute)
227+
{
228+
bool returnValue = false;
229+
230+
Thread cloneThread = new Thread(() =>
231+
{
232+
returnValue = doTheClone(installPathRoute);
233+
});
234+
cloneThread.Start();
235+
236+
while (cloneThread.IsAlive)
237+
{
238+
Stopwatch sw = Stopwatch.StartNew();
239+
240+
TotalBytes = 0;
241+
sumMB(installPathRoute);
242+
dataGridViewDownloadContent.CurrentRow.Cells[1].Value =
243+
string.Format("downloaded: {0} kB", Math.Round((double)(TotalBytes / 1024)));
244+
Refresh();
245+
246+
while ((cloneThread.IsAlive) && (sw.ElapsedMilliseconds <= 3000)) { }
247+
}
248+
249+
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = "";
250+
251+
return returnValue;
252+
}
253+
254+
private bool doTheClone(string installPathRoute)
255+
{
256+
try
257+
{
258+
Repository.Clone(Routes[RouteName].Url, installPathRoute);
259+
}
260+
catch (LibGit2SharpException libGit2SharpException)
261+
{
262+
{
263+
string message = Catalog.GetStringFmt("Error during download: {0}", libGit2SharpException.Message);
264+
MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OK, MessageBoxIcon.Error);
265+
return false;
266+
}
267+
}
268+
269+
return true;
270+
}
271+
272+
long TotalBytes = 0;
273+
274+
private void sumMB(string path)
275+
{
276+
foreach (string fileName in Directory.GetFiles(path))
277+
{
278+
TotalBytes += new System.IO.FileInfo(fileName).Length;
279+
}
280+
281+
foreach (string directoryName in Directory.GetDirectories(path))
282+
{
283+
sumMB(directoryName);
284+
}
285+
}
231286
}
287+
232288
}

0 commit comments

Comments
 (0)