Skip to content

Commit f100723

Browse files
Progress update fixed, did work in Debug mode,but not in Release mode
1 parent 6c9da3f commit f100723

File tree

3 files changed

+132
-50
lines changed

3 files changed

+132
-50
lines changed

Source/Menu/DownloadContentForm.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Menu/DownloadContentForm.cs

Lines changed: 128 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.Net;
3131
using System.IO.Compression;
3232
using System.Drawing;
33+
using System.Threading.Tasks;
3334

3435
namespace ORTS
3536
{
@@ -45,6 +46,9 @@ public partial class DownloadContentForm : Form
4546
private Thread ImageThread;
4647
private readonly string InfoTempFilename;
4748

49+
//attribute used to refresh UI
50+
private readonly SynchronizationContext SynchronizationContext;
51+
4852
public DownloadContentForm(UserSettings settings)
4953
{
5054
InitializeComponent();
@@ -72,6 +76,8 @@ public DownloadContentForm(UserSettings settings)
7276
InfoTempFilename = Path.GetTempFileName();
7377
File.Delete(InfoTempFilename);
7478
InfoTempFilename = Path.ChangeExtension(ImageTempFilename, "html");
79+
80+
SynchronizationContext = SynchronizationContext.Current;
7581
}
7682

7783
#region SelectionChanged
@@ -151,7 +157,7 @@ private void InstallPathBrowseButton_Click(object sender, EventArgs e)
151157
#endregion
152158

153159
#region DownloadContentButton
154-
private void DownloadContentButton_Click(object sender, EventArgs e)
160+
private async void DownloadContentButton_Click(object sender, EventArgs e)
155161
{
156162
ContentRouteSettings.Route route = Routes[RouteName];
157163

@@ -251,12 +257,10 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
251257

252258
// the download
253259

254-
Cursor.Current = Cursors.WaitCursor;
255-
256260
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = Catalog.GetString("Installing...");
257261
Refresh();
258262

259-
if (!downloadRoute(installPathRoute))
263+
if (!await Task.Run(() => downloadRoute(installPathRoute)))
260264
{
261265
EnableButtons();
262266
return;
@@ -298,9 +302,6 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
298302

299303
MessageBox.Show(Catalog.GetString("Route installed."),
300304
Catalog.GetString("Done"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
301-
302-
// close this dialog
303-
DialogResult = DialogResult.OK;
304305
}
305306

306307
EnableButtons();
@@ -331,11 +332,13 @@ private bool downloadRoute(string installPathRoute)
331332

332333
TotalBytes = 0;
333334
sumMB(installPathRoute);
334-
dataGridViewDownloadContent.CurrentRow.Cells[1].Value =
335-
string.Format("downloaded: {0} kB", (TotalBytes / 1024).ToString("N0"));
336-
Refresh();
335+
SynchronizationContext.Post(new SendOrPostCallback(o =>
336+
{
337+
dataGridViewDownloadContent.CurrentRow.Cells[1].Value =
338+
string.Format("Downloaded: {0} kB", (string)o);
339+
}), (TotalBytes / 1024).ToString("N0"));
337340

338-
while ((downloadThread.IsAlive) && (sw.ElapsedMilliseconds <= 3000)) { }
341+
while ((downloadThread.IsAlive) && (sw.ElapsedMilliseconds <= 1000)) { }
339342
}
340343

341344
if (returnValue)
@@ -356,16 +359,22 @@ private bool downloadRoute(string installPathRoute)
356359

357360
TotalBytes = -bytesZipfile;
358361
sumMB(installPathRoute);
359-
dataGridViewDownloadContent.CurrentRow.Cells[1].Value =
360-
string.Format("Installed: {0} kB", (TotalBytes / 1024).ToString("N0"));
361-
Refresh();
362+
SynchronizationContext.Post(new SendOrPostCallback(o =>
363+
{
364+
dataGridViewDownloadContent.CurrentRow.Cells[1].Value =
365+
string.Format("Installed: {0} kB", (string)o);
366+
}), (TotalBytes / 1024).ToString("N0"));
362367

363-
while ((installThread.IsAlive) && (sw.ElapsedMilliseconds <= 3000)) { }
368+
while ((installThread.IsAlive) && (sw.ElapsedMilliseconds <= 1000)) { }
364369
}
365370
}
366371
}
367372

368-
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = "";
373+
SynchronizationContext.Post(new SendOrPostCallback(o =>
374+
{
375+
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = "";
376+
}), "");
377+
369378

370379
return returnValue;
371380
}
@@ -428,17 +437,35 @@ private bool doTheUnzipInstall(string installPathRouteZipfileName, string instal
428437

429438
long TotalBytes = 0;
430439

431-
private void sumMB(string path)
440+
private bool sumMB(string path)
432441
{
433-
foreach (string fileName in Directory.GetFiles(path))
442+
try
434443
{
435-
TotalBytes += new FileInfo(fileName).Length;
436-
}
444+
foreach (string fileName in Directory.GetFiles(path))
445+
{
446+
try
447+
{
448+
TotalBytes += new FileInfo(fileName).Length;
449+
}
450+
catch (Exception)
451+
{
452+
// this summing is also used during the delete,
453+
// so sometimes the file is already gone
454+
}
455+
}
437456

438-
foreach (string directoryName in Directory.GetDirectories(path))
457+
foreach (string directoryName in Directory.GetDirectories(path))
458+
{
459+
sumMB(directoryName);
460+
}
461+
}
462+
catch (Exception)
439463
{
440-
sumMB(directoryName);
464+
// catch all errors during the delete
465+
return false;
441466
}
467+
468+
return true;
442469
}
443470

444471
private bool insertRowInOptions(string installPathRoute)
@@ -585,20 +612,27 @@ private void writeAndStartInfoFile()
585612
outputFile.WriteLine("- " + Catalog.GetString("Weather") + ": " + route.Start.Weather + "<br></p>");
586613
}
587614

588-
if (route.Installed && route.getDownloadType() == ContentRouteSettings.DownloadType.zip)
589-
{
590-
infoChangedAndAddedFileForZipDownloadType(route, outputFile);
591-
}
592-
if (route.Installed && route.getDownloadType() == ContentRouteSettings.DownloadType.github)
615+
if (Directory.Exists(route.DirectoryInstalledIn))
593616
{
594-
bool bothLocalAsRemoteUpdatesFound = infoChangedAndAddedFileForGitHubDownloadType(route, outputFile);
595-
if (bothLocalAsRemoteUpdatesFound)
617+
if (route.Installed && route.getDownloadType() == ContentRouteSettings.DownloadType.zip)
596618
{
597-
outputFile.WriteLine("<p><b>" + Catalog.GetString("ATTENTION: both local and remote updates found.") + "</b><br>");
598-
outputFile.WriteLine("<b>" + Catalog.GetString("Update might fail if the update tries to overwrite a changed local file.") + "</b><br>");
599-
outputFile.WriteLine("<b>" + Catalog.GetString("If that's the case you are on your own!") + "</b><br>");
600-
outputFile.WriteLine("<b>" + Catalog.GetString("Get a git expert at your desk or just Delete and Install the route again.") + "</b><br></p>");
619+
infoChangedAndAddedFileForZipDownloadType(route, outputFile);
601620
}
621+
if (route.Installed && route.getDownloadType() == ContentRouteSettings.DownloadType.github)
622+
{
623+
bool bothLocalAsRemoteUpdatesFound = infoChangedAndAddedFileForGitHubDownloadType(route, outputFile);
624+
if (bothLocalAsRemoteUpdatesFound)
625+
{
626+
outputFile.WriteLine("<p><b>" + Catalog.GetString("ATTENTION: both local and remote updates found.") + "</b><br>");
627+
outputFile.WriteLine("<b>" + Catalog.GetString("Update might fail if the update tries to overwrite a changed local file.") + "</b><br>");
628+
outputFile.WriteLine("<b>" + Catalog.GetString("If that's the case you are on your own!") + "</b><br>");
629+
outputFile.WriteLine("<b>" + Catalog.GetString("Get a git expert at your desk or just Delete and Install the route again.") + "</b><br></p>");
630+
}
631+
}
632+
}
633+
else
634+
{
635+
outputFile.WriteLine("<p>" + Catalog.GetStringFmt("Directory {0} does not exist", route.DirectoryInstalledIn) + "</b><br></p>");
602636
}
603637
}
604638
try
@@ -710,8 +744,6 @@ void StartButton_Click(object sender, EventArgs e)
710744
{
711745
DisableButtons();
712746

713-
Cursor.Current = Cursors.WaitCursor;
714-
715747
ContentRouteSettings.Route route = Routes[RouteName];
716748
string contentName = route.ContentName;
717749
MainForm mainForm = ((MainForm)Owner);
@@ -840,7 +872,7 @@ public StartNotFound(string message)
840872
#endregion
841873

842874
#region DeleteButton
843-
void DeleteButton_Click(object sender, EventArgs e)
875+
private async void DeleteButton_Click(object sender, EventArgs e)
844876
{
845877
ContentRouteSettings.Route route = Routes[RouteName];
846878
string message;
@@ -855,23 +887,22 @@ void DeleteButton_Click(object sender, EventArgs e)
855887
return;
856888
}
857889

858-
Cursor.Current = Cursors.WaitCursor;
890+
if (Directory.Exists(route.DirectoryInstalledIn)) {
859891

860-
if (areThereChangedAddedFiles(route))
861-
{
862-
writeAndStartInfoFile();
863-
message = Catalog.GetStringFmt("Changed or added local files found in Directory \"{0}\", see Info at the bottom for more information. Do you want to continue?", route.DirectoryInstalledIn);
864-
if (MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) != DialogResult.OK)
892+
if (areThereChangedAddedFiles(route))
865893
{
866-
// cancelled
867-
EnableButtons();
868-
return;
894+
writeAndStartInfoFile();
895+
message = Catalog.GetStringFmt("Changed or added local files found in Directory \"{0}\", see Info at the bottom for more information. Do you want to continue?", route.DirectoryInstalledIn);
896+
if (MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) != DialogResult.OK)
897+
{
898+
// cancelled
899+
EnableButtons();
900+
return;
901+
}
869902
}
870-
}
871-
872-
Cursor.Current = Cursors.WaitCursor;
873903

874-
ContentRouteSettings.directoryDelete(route.DirectoryInstalledIn);
904+
await Task.Run(() => deleteRoute(route.DirectoryInstalledIn));
905+
}
875906

876907
if (Settings.Folders.Folders[route.ContentName] == route.ContentDirectory)
877908
{
@@ -891,6 +922,33 @@ void DeleteButton_Click(object sender, EventArgs e)
891922

892923
EnableButtons();
893924
}
925+
926+
private void deleteRoute(string directoryInstalledIn)
927+
{
928+
Thread deleteThread = new Thread(() =>
929+
{
930+
ContentRouteSettings.directoryDelete(directoryInstalledIn);
931+
});
932+
// start download in thread to be able to show the progress in the main thread
933+
deleteThread.Start();
934+
935+
while (deleteThread.IsAlive)
936+
{
937+
Stopwatch sw = Stopwatch.StartNew();
938+
939+
TotalBytes = 0;
940+
if (sumMB(directoryInstalledIn))
941+
{
942+
SynchronizationContext.Post(new SendOrPostCallback(o =>
943+
{
944+
dataGridViewDownloadContent.CurrentRow.Cells[1].Value =
945+
string.Format("Left: {0} kB", (string)o);
946+
}), (TotalBytes / 1024).ToString("N0"));
947+
}
948+
949+
while ((deleteThread.IsAlive) && (sw.ElapsedMilliseconds <= 1000)) { }
950+
}
951+
}
894952
#endregion
895953

896954
#region UpdateButton
@@ -957,6 +1015,13 @@ private bool doThePull(ContentRouteSettings.Route route) {
9571015

9581016
private void DisableButtons()
9591017
{
1018+
UseWaitCursor = true;
1019+
1020+
dataGridViewDownloadContent.Enabled = false;
1021+
InstallPathTextBox.Enabled = false;
1022+
InstallPathBrowseButton.Enabled = false;
1023+
infoButton.Enabled = false;
1024+
9601025
downloadContentButton.Enabled = false;
9611026
startButton.Enabled = false;
9621027
deleteButton.Enabled = false;
@@ -967,6 +1032,12 @@ private void EnableButtons()
9671032
{
9681033
ContentRouteSettings.Route route = Routes[RouteName];
9691034

1035+
UseWaitCursor = false;
1036+
1037+
dataGridViewDownloadContent.Enabled = true;
1038+
InstallPathTextBox.Enabled = true;
1039+
InstallPathBrowseButton.Enabled = true;
1040+
infoButton.Enabled = true;
9701041
downloadContentButton.Enabled = !route.Installed;
9711042
startButton.Enabled = route.Installed && !string.IsNullOrWhiteSpace(route.Start.Route);
9721043
deleteButton.Enabled = route.Installed;
@@ -1143,8 +1214,15 @@ private List<string> getCommits(string installPathRoute)
11431214
return commits;
11441215
}
11451216

1146-
private void DownloadContentForm_FormClosing(object sender, FormClosingEventArgs e)
1217+
private void DownloadContentForm_FormClosing(object sender, FormClosingEventArgs formClosingEventArgs)
11471218
{
1219+
if (UseWaitCursor)
1220+
{
1221+
// cancelled event, so continue
1222+
formClosingEventArgs.Cancel = true;
1223+
return;
1224+
}
1225+
11481226
try
11491227
{
11501228
if (pictureBoxRoute.Image != null)

Source/Menu/DownloadContentForm.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,7 @@
120120
<metadata name="InstallPathDirectoryEntry.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
121121
<value>17, 17</value>
122122
</metadata>
123+
<metadata name="textBoxRoute.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
124+
<value>True</value>
125+
</metadata>
123126
</root>

0 commit comments

Comments
 (0)