Skip to content
This repository was archived by the owner on Dec 22, 2019. It is now read-only.

Commit ea7b341

Browse files
authored
Merge pull request #20 from MatthiWare/development
Performance & bug fixes
2 parents c5a03fb + caaf7f7 commit ea7b341

29 files changed

+900
-162
lines changed

UpdateLib/TestApp/Form1.Designer.cs

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

UpdateLib/TestApp/Form1.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Text;
1111
using System.Threading;
1212
using System.Windows.Forms;
13+
using TestApp.Testing;
1314

1415
namespace TestApp
1516
{
@@ -65,14 +66,42 @@ private void Form1_Load(object sender, EventArgs e)
6566
{
6667
label1.Text = ReadFile("data/testfile1.txt");
6768
label2.Text = ReadFile("data/testfile2.txt");
68-
label3.Text = ReadFile("data/testfile3.txt");
69+
label3.Text = ReadFileAndKeepStreamOpen("data/testfile3.txt");
6970
}
7071

7172
private string ReadFile(string file)
7273
{
74+
if (!File.Exists(file))
75+
return "ERROR: File doesn't exist..";
76+
7377
string[] lines = File.ReadAllLines(file);
7478

75-
return string.Join(", ", lines);
79+
return string.Join(", ", lines);
80+
}
81+
82+
/// <summary>
83+
/// Bad code that keeps the file open & locked
84+
/// Purpose: to demonstrate the updater still works on locked files.
85+
/// </summary>
86+
/// <param name="file"></param>
87+
/// <returns></returns>
88+
private string ReadFileAndKeepStreamOpen(string file)
89+
{
90+
if (!File.Exists(file))
91+
return "ERROR: File doesn't exist..";
92+
93+
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
94+
StreamReader sr = new StreamReader(fs);
95+
string text = sr.ReadToEnd();
96+
97+
return text;
98+
}
99+
100+
private void button1_Click(object sender, EventArgs e)
101+
{
102+
DummyTask task = new DummyTask();
103+
task.TaskCompleted += (o, ex) => Logger.Debug(nameof(DummyTask), "Callback task completed!");
104+
task.Start();
76105
}
77106
}
78107
}

UpdateLib/TestApp/TestApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
</Compile>
5555
<Compile Include="Program.cs" />
5656
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<Compile Include="Testing\DummyTask.cs" />
5758
<EmbeddedResource Include="Form1.resx">
5859
<DependentUpon>Form1.cs</DependentUpon>
5960
</EmbeddedResource>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using MatthiWare.UpdateLib.Logging;
2+
using MatthiWare.UpdateLib.Tasks;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading;
8+
9+
namespace TestApp.Testing
10+
{
11+
public class DummyTask : AsyncTask
12+
{
13+
protected override void DoWork()
14+
{
15+
for (int i = 0; i < 10; i++)
16+
{
17+
Enqueue(new Action<int>(ChildWorkStuff), i);
18+
}
19+
}
20+
21+
Random rnd = new Random(DateTime.Now.Millisecond);
22+
23+
private void ChildWorkStuff(int id)
24+
{
25+
int waitTime = rnd.Next(1000, 5000);
26+
27+
Thread.Sleep(waitTime);
28+
29+
Logger.Debug(nameof(ChildWorkStuff), $"Task[{id.ToString("X2")}] Completed");
30+
}
31+
32+
33+
}
34+
}

UpdateLib/UpdateLib.Generator/Tasks/UpdateGeneratorTask.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using MatthiWare.UpdateLib.Generator.Data.FilesPage;
88
using System.Collections.Generic;
99
using MatthiWare.UpdateLib.Generator.UI.Pages;
10+
using MatthiWare.UpdateLib.Logging;
1011

1112
namespace MatthiWare.UpdateLib.Generator.Tasks
1213
{
@@ -53,6 +54,8 @@ protected override void DoWork()
5354

5455
private void AddDirRecursive(GenFolder dir, DirectoryEntry entry)
5556
{
57+
// Logger.Debug(GetType().Name, $"Thread: {Thread.CurrentThread.ManagedThreadId}");
58+
5659
List<GenFile> files = dir.Files;
5760
foreach (GenFile genFile in files)
5861
{
@@ -78,6 +81,8 @@ private void AddDirRecursive(GenFolder dir, DirectoryEntry entry)
7881

7982
AddDirRecursiveDelegate caller = new AddDirRecursiveDelegate(AddDirRecursive);
8083
Enqueue(caller, newDir, newEntry);
84+
85+
//AddDirRecursive(newDir, newEntry);
8186
}
8287
}
8388
}

UpdateLib/UpdateLib.Generator/TestForm.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private AsyncTask LoadPagesTask()
6464
{
6565
LoaderControl.Hide(ContentPanel);
6666

67-
this.InvokeOnUI(() => btnTabInformation.PerformClick());
67+
btnTabInformation.PerformClick();
6868
};
6969
}
7070

@@ -161,38 +161,34 @@ private bool LoadPage(string pageName)
161161

162162
private void ShowMessageBox(string title, string header, string desc, Icon icon, MessageBoxButtons buttons = MessageBoxButtons.YesNo)
163163
{
164-
this.InvokeOnUI(() =>
165-
{
166-
MessageDialog.Show(
167-
title,
168-
header,
169-
desc,
170-
icon,
171-
buttons);
172-
});
164+
MessageDialog.Show(
165+
this,
166+
title,
167+
header,
168+
desc,
169+
icon,
170+
buttons);
173171
}
174172

175173
private void AddControlToContentPanel(Control toAdd)
176174
{
177175
if (!shouldShowNewPage)
178176
return;
179177

180-
this.InvokeOnUI(() =>
181-
{
182-
ContentPanel.SuspendLayout();
178+
ContentPanel.SuspendLayout();
183179

184-
ContentPanel.Controls.Clear();
180+
ContentPanel.Controls.Clear();
185181

186-
if (toAdd != null)
187-
{
188-
toAdd.Dock = DockStyle.Fill;
189-
ContentPanel.Controls.Add(toAdd);
182+
if (toAdd != null)
183+
{
184+
toAdd.Dock = DockStyle.Fill;
185+
ContentPanel.Controls.Add(toAdd);
190186

191-
shouldShowNewPage = false;
192-
}
187+
shouldShowNewPage = false;
188+
}
189+
190+
ContentPanel.ResumeLayout();
193191

194-
ContentPanel.ResumeLayout();
195-
});
196192
}
197193

198194
private void btnTabBuild_Click(object sender, EventArgs e)

UpdateLib/UpdateLib.Generator/UI/Pages/BuilderPage.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.

UpdateLib/UpdateLib.Generator/UI/Pages/BuilderPage.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using MatthiWare.UpdateLib.Tasks;
1313
using MatthiWare.UpdateLib.Generator.Tasks;
1414
using MatthiWare.UpdateLib.Files;
15+
using System.Diagnostics;
16+
using MatthiWare.UpdateLib.Logging;
1517

1618
namespace MatthiWare.UpdateLib.Generator.UI.Pages
1719
{
@@ -27,7 +29,7 @@ public BuilderPage()
2729

2830
protected override void OnPageInitialize()
2931
{
30-
saveFileDialog.InitialDirectory = new DirectoryInfo("./Output").FullName;
32+
saveFileDialog.InitialDirectory = new DirectoryInfo("./Output").FullName;
3133

3234
PageControlBase page;
3335
if (!TestForm.TryGetPage(nameof(FilesPage), out page))
@@ -62,6 +64,8 @@ private void btnBuild_Click(object sender, EventArgs e)
6264
Build(saveFileDialog.OpenFile());
6365
}
6466

67+
Stopwatch sw = new Stopwatch();
68+
6569
private AsyncTask<UpdateFile> Build(Stream s)
6670
{
6771
UpdateGeneratorTask task = new UpdateGeneratorTask(filesPage.Root, infoPage);
@@ -70,27 +74,30 @@ private AsyncTask<UpdateFile> Build(Stream s)
7074

7175
task.TaskProgressChanged += (o, e) =>
7276
{
73-
this.InvokeOnUI(() =>
74-
{
75-
lblProgress.Text = $"Progress: {e.ProgressPercentage}%";
76-
pbProgress.Value = e.ProgressPercentage;
77-
});
77+
lblProgress.Text = $"Progress: {e.ProgressPercentage}%";
78+
pbProgress.Value = e.ProgressPercentage;
79+
7880
};
7981

8082
task.TaskCompleted += (o, e) =>
8183
{
84+
sw.Stop();
85+
86+
Logger.Debug(GetType().Name, $"File generation completed in {sw.ElapsedMilliseconds} ms.");
87+
88+
8289

83-
this.InvokeOnUI(() => btnBuild.Enabled = true);
90+
btnBuild.Enabled = true;
8491

8592
if (e.Cancelled)
8693
{
87-
this.InvokeOnUI(() => lblStatus.Text = $"Status: Cancelled");
94+
lblStatus.Text = $"Status: Cancelled";
8895
return;
8996
}
9097

9198
if (e.Error != null)
9299
{
93-
this.InvokeOnUI(() => lblStatus.Text = $"Status: Error");
100+
lblStatus.Text = $"Status: Error";
94101

95102
MessageDialog.Show(
96103
ParentForm,
@@ -106,11 +113,23 @@ private AsyncTask<UpdateFile> Build(Stream s)
106113
using (s)
107114
task.Result.Save(s);
108115

109-
this.InvokeOnUI(() => lblStatus.Text = $"Status: Completed");
116+
lblStatus.Text = $"Status: Completed";
117+
118+
MessageDialog.Show(
119+
ParentForm,
120+
"Builder",
121+
"Build completed",
122+
"The update file has been succesfully generated!\n" +
123+
$"File generation completed in {sw.ElapsedMilliseconds} ms.",
124+
SystemIcons.Information,
125+
MessageBoxButtons.OK);
110126
};
111127

112128
lblStatus.Text = "Status: Building..";
113129

130+
sw.Reset();
131+
sw.Start();
132+
114133
return task.Start();
115134
}
116135
}

UpdateLib/UpdateLib.Generator/UI/Pages/InformationPage.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.

UpdateLib/UpdateLib.Generator/UI/Pages/PageControlBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,16 @@ public AsyncTask InitializePage(EventHandler<AsyncCompletedEventArgs> callBack)
5050

5151
protected virtual void OnPageInitialize() { }
5252

53+
private void InitializeComponent()
54+
{
55+
this.SuspendLayout();
56+
//
57+
// PageControlBase
58+
//
59+
this.DoubleBuffered = true;
60+
this.Name = "PageControlBase";
61+
this.ResumeLayout(false);
62+
63+
}
5364
}
5465
}

0 commit comments

Comments
 (0)