Skip to content

Commit 02cc4b7

Browse files
authored
Merge pull request #7 from temotskipa/fix-large-folder-hang
Fix UI hanging when adding large folders
2 parents 7da1af2 + d0bc0b9 commit 02cc4b7

2 files changed

Lines changed: 124 additions & 86 deletions

File tree

GDeflateConsole/Program.cs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -330,29 +330,14 @@ static void TestCompressionDecompression(string testFile, GDeflateProcessor proc
330330
Console.WriteLine($" Decompressed size: {FormatFileSize(decompressedInfo.Length)}");
331331
Console.WriteLine($" Decompression time: {decompressTime.TotalMilliseconds:F2} ms");
332332

333-
// Verify integrity (for simulation mode)
334-
if (processor.IsSimulationMode)
333+
// Verify integrity
334+
Console.WriteLine(" Verifying file integrity...");
335+
bool filesMatch = AreFilesEqual(testFile, decompressedFile);
336+
Console.WriteLine($" Integrity check: {(filesMatch ? "PASSED" : "FAILED")}");
337+
338+
if (!filesMatch)
335339
{
336-
Console.WriteLine($" Integrity check: Simulation mode (basic verification)");
337-
}
338-
else
339-
{
340-
// For real GPU mode, we could do byte-by-byte comparison
341-
bool identical = originalInfo.Length == decompressedInfo.Length;
342-
if (identical && originalInfo.Length < 10 * 1024 * 1024) // Only for files < 10MB
343-
{
344-
var originalBytes = File.ReadAllBytes(testFile);
345-
var decompressedBytes = File.ReadAllBytes(decompressedFile);
346-
347-
for (int i = 0; i < originalBytes.Length && identical; i++)
348-
{
349-
if (originalBytes[i] != decompressedBytes[i])
350-
{
351-
identical = false;
352-
}
353-
}
354-
}
355-
Console.WriteLine($" Integrity check: {(identical ? "PASSED" : "FAILED")}");
340+
throw new Exception("File integrity check failed: decompressed file does not match original.");
356341
}
357342

358343
Console.WriteLine(" Test PASSED");
@@ -372,6 +357,44 @@ static void TestCompressionDecompression(string testFile, GDeflateProcessor proc
372357
}
373358
}
374359

360+
static bool AreFilesEqual(string path1, string path2)
361+
{
362+
const int bufferSize = 4096;
363+
364+
using (var fs1 = new FileStream(path1, FileMode.Open, FileAccess.Read))
365+
using (var fs2 = new FileStream(path2, FileMode.Open, FileAccess.Read))
366+
{
367+
if (fs1.Length != fs2.Length)
368+
{
369+
return false;
370+
}
371+
372+
var buffer1 = new byte[bufferSize];
373+
var buffer2 = new byte[bufferSize];
374+
375+
while (true)
376+
{
377+
int bytesRead1 = fs1.Read(buffer1, 0, bufferSize);
378+
int bytesRead2 = fs2.Read(buffer2, 0, bufferSize);
379+
380+
if (bytesRead1 != bytesRead2)
381+
{
382+
return false;
383+
}
384+
385+
if (bytesRead1 == 0)
386+
{
387+
return true;
388+
}
389+
390+
if (!buffer1.AsSpan(0, bytesRead1).SequenceEqual(buffer2.AsSpan(0, bytesRead2)))
391+
{
392+
return false;
393+
}
394+
}
395+
}
396+
}
397+
375398
static string FormatFileSize(long bytes)
376399
{
377400
string[] suffixes = { "B", "KB", "MB", "GB", "TB" };

GDeflateGUI/MainForm.cs

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -138,72 +138,87 @@ await System.Threading.Tasks.Task.Run(() =>
138138

139139
private void btnAddFiles_Click(object? sender, System.EventArgs e)
140140
{
141+
if (!IsWindows)
142+
{
143+
AddFilesAlternativeMethod();
144+
return;
145+
}
146+
141147
try
142148
{
143-
if (IsWindows)
149+
using (var dialog = new OpenFileDialog
144150
{
145-
// Use Windows Forms dialog on Windows
146-
using (var dialog = new OpenFileDialog())
151+
Multiselect = true,
152+
Title = "Select files",
153+
Filter = "All files (*.*)|*.*"
154+
})
155+
{
156+
if (dialog.ShowDialog() == DialogResult.OK)
147157
{
148-
dialog.Multiselect = true;
149-
dialog.Title = "Select files";
150-
dialog.Filter = "All files (*.*)|*.*";
151-
if (dialog.ShowDialog() == DialogResult.OK)
152-
{
153-
foreach (string file in dialog.FileNames)
154-
{
155-
AddFileToList(file);
156-
}
157-
UpdateStatus($"Added {dialog.FileNames.Length} files. Total: {listViewFiles.Items.Count}");
158-
}
158+
AddFilesToList(dialog.FileNames);
159159
}
160160
}
161-
else
162-
{
163-
// Alternative method for non-Windows platforms
164-
AddFilesAlternativeMethod();
165-
}
166161
}
167162
catch (Exception ex)
168163
{
169-
MessageBox.Show($"Error adding files: {ex.Message}\n\nTip: On non-Windows platforms, you can manually enter file paths or use the alternative method.",
170-
"File Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
171-
UpdateStatus("Error occurred while adding files. See message for details.");
164+
ShowError("Error adding files", ex);
172165
}
173166
}
174167

175-
private void btnAddFolder_Click(object? sender, System.EventArgs e)
168+
private async void btnAddFolder_Click(object? sender, System.EventArgs e)
176169
{
170+
if (!IsWindows)
171+
{
172+
AddFolderAlternativeMethod();
173+
return;
174+
}
175+
177176
try
178177
{
179-
if (IsWindows)
178+
using (var dialog = new FolderBrowserDialog
179+
{
180+
Description = "Select a folder to add all its files"
181+
})
180182
{
181-
// Use Windows Forms dialog on Windows
182-
using (var dialog = new FolderBrowserDialog())
183+
if (dialog.ShowDialog() == DialogResult.OK)
183184
{
184-
dialog.Description = "Select a folder to add all its files";
185-
if (dialog.ShowDialog() == DialogResult.OK)
186-
{
187-
var files = Directory.GetFiles(dialog.SelectedPath, "*.*", SearchOption.AllDirectories);
188-
foreach (string file in files)
189-
{
190-
AddFileToList(file);
191-
}
192-
UpdateStatus($"Added {files.Length} files from folder. Total: {listViewFiles.Items.Count}");
193-
}
185+
await AddFolderFilesAsync(dialog.SelectedPath);
194186
}
195187
}
196-
else
197-
{
198-
// Alternative method for non-Windows platforms
199-
AddFolderAlternativeMethod();
200-
}
201188
}
202189
catch (Exception ex)
203190
{
204-
MessageBox.Show($"Error adding folder: {ex.Message}\n\nTip: On non-Windows platforms, you can manually enter folder paths or use the alternative method.",
205-
"Folder Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
206-
UpdateStatus("Error occurred while adding folder. See message for details.");
191+
ShowError("Error adding folder", ex);
192+
}
193+
}
194+
195+
private async System.Threading.Tasks.Task AddFolderFilesAsync(string path)
196+
{
197+
SetUIEnabled(false);
198+
UpdateStatus("Searching for files...");
199+
200+
try
201+
{
202+
var files = await System.Threading.Tasks.Task.Run(() =>
203+
Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
204+
);
205+
AddFilesToList(files);
206+
}
207+
catch (UnauthorizedAccessException ex)
208+
{
209+
ShowError("Access denied. You may not have permission to access all subdirectories.", ex);
210+
}
211+
catch (IOException ex)
212+
{
213+
ShowError("An I/O error occurred while accessing the folder.", ex);
214+
}
215+
catch (Exception ex)
216+
{
217+
ShowError("An unexpected error occurred while adding folder files.", ex);
218+
}
219+
finally
220+
{
221+
SetUIEnabled(true);
207222
}
208223
}
209224

@@ -237,16 +252,11 @@ private void AddFilesAlternativeMethod()
237252
.Take(5) // Limit to first 5 files
238253
.ToArray();
239254

240-
foreach (string file in files)
241-
{
242-
AddFileToList(file);
243-
}
244-
245-
UpdateStatus($"Added {files.Length} test files from current directory. Total: {listViewFiles.Items.Count}");
255+
AddFilesToList(files);
246256
}
247257
catch (Exception ex)
248258
{
249-
MessageBox.Show($"Error adding test files: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
259+
ShowError("Error adding test files", ex);
250260
}
251261
}
252262
}
@@ -271,31 +281,36 @@ private void AddFolderAlternativeMethod()
271281
{
272282
var currentDir = Directory.GetCurrentDirectory();
273283
var files = Directory.GetFiles(currentDir, "*.*", SearchOption.AllDirectories);
274-
275-
foreach (string file in files)
276-
{
277-
AddFileToList(file);
278-
}
279-
280-
UpdateStatus($"Added {files.Length} files from current directory and subdirectories. Total: {listViewFiles.Items.Count}");
284+
AddFilesToList(files);
281285
}
282286
catch (Exception ex)
283287
{
284-
MessageBox.Show($"Error adding files from folder: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
288+
ShowError("Error adding files from folder", ex);
285289
}
286290
}
287291
}
288292

289-
private void AddFileToList(string filePath)
293+
private void AddFilesToList(string[] filePaths)
290294
{
291-
// Check if the file is already in the list to avoid duplicates
292-
if (!listViewFiles.Items.Cast<ListViewItem>().Any(item => item.Text == filePath))
295+
var newItems = filePaths
296+
.Where(filePath => !listViewFiles.Items.Cast<ListViewItem>().Any(item => item.Text == filePath))
297+
.Select(filePath => new ListViewItem(filePath))
298+
.ToArray();
299+
300+
if (newItems.Any())
293301
{
294-
var item = new ListViewItem(filePath);
295-
listViewFiles.Items.Add(item);
302+
listViewFiles.Items.AddRange(newItems);
303+
UpdateStatus($"Added {newItems.Length} files. Total: {listViewFiles.Items.Count}");
296304
}
297305
}
298306

307+
private void ShowError(string title, Exception ex)
308+
{
309+
MessageBox.Show($"{ex.Message}\n\nTip: On non-Windows platforms, you can use the console version for more detailed error information.",
310+
title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
311+
UpdateStatus($"Error: {title}. See message for details.");
312+
}
313+
299314
private void UpdateStatus(string text)
300315
{
301316
statusLabel.Text = text;

0 commit comments

Comments
 (0)