Skip to content

Commit 88b7ff5

Browse files
committed
Finalized
1 parent c5a6bda commit 88b7ff5

6 files changed

+71
-91
lines changed

FolderCompare.cs

+16-22
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,52 @@ public class FolderCompare
2020
public static bool CheckFolderB = false;
2121
public static bool checkExisting = false;
2222

23-
public static FolderCompareWin form = FolderCompareWin.GetInstance();
24-
25-
public static void UpdateLogMessage(string message)
23+
public static async Task CompareAsync()
2624
{
27-
FolderCompareWin form = FolderCompareWin.GetInstance();
28-
29-
// Check if invoke is required for thread safety
30-
if (form.InvokeRequired)
31-
{
32-
form.Invoke(new Action(() => form.LogsBox.Items.Add(message)));
33-
}
34-
else
35-
{
36-
form.LogsBox.Items.Add(message);
37-
}
25+
await Task.Run(() => Compare());
3826
}
27+
3928
public static void Compare()
4029
{
30+
var form = FolderCompareWin.GetInstance();
4131
if (FolderA == "" || FolderB == "" || FolderExport == "")
4232
{
4333
MessageBox.Show("Please select folders and export path.");
44-
UpdateLogMessage("Please select folders and export path.");
34+
form.UpdateLogMessage("Please select folders and export path.");
4535
return;
4636
}
4737
if (FolderA == FolderB || FolderA == FolderExport || FolderB == FolderExport)
4838
{
4939
MessageBox.Show("Please select different folders.");
50-
UpdateLogMessage("Please select different folders.");
40+
form.UpdateLogMessage("Please select different folders.");
5141
return;
5242
}
5343

5444
try
5545
{
5646
var stopwatch = new Stopwatch();
47+
stopwatch.Start();
5748

5849
CompareAndCopy(FolderA, FolderB, FolderExport, CheckFolderA, CheckFolderB, checkExisting);
5950

6051
stopwatch.Stop();
6152

62-
UpdateLogMessage("Comparing and copying Sucess!");
63-
UpdateLogMessage("time: " + FormatElapsedTime(stopwatch.Elapsed));
53+
var elapsedTime = stopwatch.Elapsed;
54+
form.UpdateLogMessage("Comparing and copying Sucess!");
55+
form.UpdateLogMessage("time: " + FormatElapsedTime(elapsedTime));
6456
}
6557
catch (Exception ex)
6658
{
6759
MessageBox.Show("Error: \n" + ex.Message);
68-
UpdateLogMessage("Error: \n" + ex.Message);
60+
form.UpdateLogMessage("Error: \n" + ex.Message);
6961
return;
7062
}
7163

7264
}
7365

7466
static void CompareAndCopy(string sourceDir, string targetDir, string destDir, bool copyIfNotInSource, bool copyIfNotInTarget, bool replaceExistingFiles)
7567
{
68+
var form = FolderCompareWin.GetInstance();
7669
DirectoryInfo dir1 = new DirectoryInfo(sourceDir);
7770
DirectoryInfo dir2 = new DirectoryInfo(targetDir);
7871

@@ -103,7 +96,7 @@ static void CompareAndCopy(string sourceDir, string targetDir, string destDir, b
10396
catch (Exception ex)
10497
{
10598
MessageBox.Show("Error in process file: \n" + ex.Message);
106-
UpdateLogMessage("Error in process file: \n" + ex.Message);
99+
form.UpdateLogMessage("Error in process file: \n" + ex.Message);
107100
}
108101
});
109102

@@ -124,14 +117,15 @@ static void CompareAndCopy(string sourceDir, string targetDir, string destDir, b
124117
catch (Exception ex)
125118
{
126119
MessageBox.Show("Error in process file: \n" + ex.Message);
127-
UpdateLogMessage("Error in process file: \n" + ex.Message);
120+
form.UpdateLogMessage("Error in process file: \n" + ex.Message);
128121
}
129122
});
130123
}
131124
}
132125

133126
private static void CopyFile(string sourceFilePath, string destDir, string relativePath, bool replaceExistingFiles)
134127
{
128+
var form = FolderCompareWin.GetInstance();
135129
string destFilePath = Path.Combine(destDir, relativePath);
136130
string destFileDir = Path.GetDirectoryName(destFilePath);
137131

@@ -144,7 +138,7 @@ private static void CopyFile(string sourceFilePath, string destDir, string relat
144138
{
145139
File.Copy(sourceFilePath, destFilePath, replaceExistingFiles);
146140
}
147-
UpdateLogMessage("File Copy: " + relativePath);
141+
form.UpdateLogMessage("File Copy: " + relativePath);
148142
}
149143

150144
private static bool FileContentsAreEqual(string file1Path, string file2Path)

FolderCompareWin.Designer.cs

+13-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FolderCompareWin.cs

+31-41
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,43 @@ private void buttonA_Click(object sender, EventArgs e)
1515
{
1616
FolderCompare.FolderA = folderA.SelectedPath;
1717
textBoxA.Text = folderA.SelectedPath;
18+
UpdateLogMessage("Folder A: " + folderA.SelectedPath);
1819
}
1920
}
2021

21-
private void textBoxA_TextChanged(object sender, EventArgs e)
22-
{
23-
24-
}
25-
2622
private void buttonB_Click(object sender, EventArgs e)
2723
{
2824
folderB.ShowDialog();
2925
if (folderB.SelectedPath != null)
3026
{
3127
FolderCompare.FolderB = folderB.SelectedPath;
3228
textBoxB.Text = folderB.SelectedPath;
29+
UpdateLogMessage("Folder B: " + folderB.SelectedPath);
3330
}
3431
}
3532

36-
private void textBoxB_TextChanged(object sender, EventArgs e)
37-
{
38-
39-
}
40-
4133
private void buttonExport_Click(object sender, EventArgs e)
4234
{
4335
folderExport.ShowDialog();
4436
if (folderExport.SelectedPath != null)
4537
{
4638
FolderCompare.FolderExport = folderExport.SelectedPath;
4739
textBoxExport.Text = folderExport.SelectedPath;
40+
UpdateLogMessage("Folder Export: " + folderExport.SelectedPath);
4841
}
4942
}
5043

51-
private void textBoxExport_TextChanged(object sender, EventArgs e)
52-
{
53-
54-
}
55-
5644
private void checkFolderA_CheckedChanged(object sender, EventArgs e)
5745
{
5846
if(checkFolderA.Checked == true)
5947
{
6048
FolderCompare.CheckFolderA = true;
49+
UpdateLogMessage("Check Folder A: " + FolderCompare.CheckFolderA);
6150
}
6251
else
6352
{
6453
FolderCompare.CheckFolderA = false;
54+
UpdateLogMessage("Check Folder A: " + FolderCompare.CheckFolderA);
6555
}
6656
}
6757

@@ -70,10 +60,12 @@ private void checkFolderB_CheckedChanged(object sender, EventArgs e)
7060
if (checkFolderB.Checked == true)
7161
{
7262
FolderCompare.CheckFolderB = true;
63+
UpdateLogMessage("Check Folder B: " + FolderCompare.CheckFolderB);
7364
}
7465
else
7566
{
7667
FolderCompare.CheckFolderB = false;
68+
UpdateLogMessage("Check Folder B: " + FolderCompare.CheckFolderB);
7769
}
7870
}
7971

@@ -82,53 +74,51 @@ private void checkExisting_CheckedChanged(object sender, EventArgs e)
8274
if (checkExisting.Checked == true)
8375
{
8476
FolderCompare.checkExisting = true;
77+
UpdateLogMessage("Check Existing: " + FolderCompare.checkExisting);
8578
}
8679
else
8780
{
8881
FolderCompare.checkExisting = false;
82+
UpdateLogMessage("Check Existing: " + FolderCompare.checkExisting);
8983
}
9084
}
9185

92-
private void buttonConfirm_Click(object sender, EventArgs e)
93-
{
94-
FolderCompare.Compare();
95-
}
96-
97-
private void logsBox_SelectedIndexChanged(object sender, EventArgs e)
86+
private async void buttonConfirm_Click(object sender, EventArgs e)
9887
{
99-
88+
UpdateLogMessage("Comparing and copying Init!");
89+
await FolderCompare.CompareAsync();
90+
UpdateLogMessage("Comparing and copying Finished!");
10091
}
10192

102-
private void folderA_HelpRequest(object sender, EventArgs e)
93+
public void UpdateLogMessage(string message)
10394
{
104-
95+
// This method ensures that the update is performed on the UI thread
96+
ThreadSafeUpdate(() => textBox1.AppendText(message + Environment.NewLine));
10597
}
10698

107-
private void folderB_HelpRequest(object sender, EventArgs e)
99+
private void ThreadSafeUpdate(Action updateAction)
108100
{
109-
101+
if (this.InvokeRequired)
102+
{
103+
// If we're not on the UI thread, use Invoke to execute the updateAction on the UI thread
104+
this.Invoke(updateAction);
105+
}
106+
else
107+
{
108+
// If we're on the UI thread, execute the updateAction directly
109+
updateAction();
110+
}
110111
}
111112

112-
private void folderExport_HelpRequest(object sender, EventArgs e)
113+
public static FolderCompareWin GetInstance()
113114
{
114-
115+
return instance;
115116
}
116117

117-
public ListBox LogsBox
118+
public static void SetInstance(FolderCompareWin instance)
118119
{
119-
get { return logsBox; } // Assuming logsBox is the name of your ListBox control
120+
FolderCompareWin.instance = instance;
120121
}
121122

122-
public static FolderCompareWin GetInstance()
123-
{
124-
// If the instance doesn't exist, create it
125-
if (instance == null)
126-
{
127-
instance = new FolderCompareWin();
128-
}
129-
130-
// Return the instance
131-
return instance;
132-
}
133123
}
134124
}

FolderCompareWinForms.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<UseWindowsForms>true</UseWindowsForms>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<ApplicationIcon>compare-file-icon.ico</ApplicationIcon>
10+
<Platforms>x64</Platforms>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

FolderCompareWinForms.sln

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.8.34330.188
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FolderCompareWinForms", "FolderCompareWinForms.csproj", "{A3756B22-C49C-4AF5-B909-49903A715D69}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FolderCompareWinForms", "FolderCompareWinForms.csproj", "{A3756B22-C49C-4AF5-B909-49903A715D69}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10-
Debug|Any CPU = Debug|Any CPU
11-
Release|Any CPU = Release|Any CPU
10+
Debug|x64 = Debug|x64
11+
Release|x64 = Release|x64
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{A3756B22-C49C-4AF5-B909-49903A715D69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{A3756B22-C49C-4AF5-B909-49903A715D69}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{A3756B22-C49C-4AF5-B909-49903A715D69}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{A3756B22-C49C-4AF5-B909-49903A715D69}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{A3756B22-C49C-4AF5-B909-49903A715D69}.Debug|x64.ActiveCfg = Debug|x64
15+
{A3756B22-C49C-4AF5-B909-49903A715D69}.Debug|x64.Build.0 = Debug|x64
16+
{A3756B22-C49C-4AF5-B909-49903A715D69}.Release|x64.ActiveCfg = Release|x64
17+
{A3756B22-C49C-4AF5-B909-49903A715D69}.Release|x64.Build.0 = Release|x64
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE

Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ internal static class Program
88
[STAThread]
99
static void Main()
1010
{
11-
// To customize application configuration such as set high DPI settings or default font,
12-
// see https://aka.ms/applicationconfiguration.
1311
ApplicationConfiguration.Initialize();
14-
Application.Run(new FolderCompareWin());
12+
FolderCompareWin mainForm = new FolderCompareWin();
13+
FolderCompareWin.SetInstance(mainForm); // Set the instance before the form is shown
14+
Application.Run(mainForm);
1515
}
1616
}
1717
}

0 commit comments

Comments
 (0)