Skip to content

Commit 0b020ba

Browse files
committed
Create multiple temp files instead of waiting for mutex
1 parent d21b642 commit 0b020ba

File tree

3 files changed

+37
-71
lines changed

3 files changed

+37
-71
lines changed

src/ContextMenuUploader/Program.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace ContextMenuUploader;
99
public class Program
1010
{
1111
private const string AppName = "ContextMenuUploader";
12-
private const int BatchTimeoutMs = 1000; // Timeout in milliseconds to wait for more files to be selected
12+
private const int BatchTimeoutMs = 500; // Timeout in milliseconds to wait for more files to be selected
1313
private const string ContextMenuLabel = "Upload to web service";
1414
private const string MutexName = "Global\\ContextMenuUploader";
15-
private static readonly string TempFilePath = Path.Combine(Path.GetTempPath(), "ContextMenuUploader.txt");
15+
private static readonly string TempFilePath = Path.Combine(Path.GetTempPath(), "ContextMenuUploader");
1616

1717
[STAThread]
1818
public static async Task Main(string[] args)
@@ -44,8 +44,12 @@ public static async Task Main(string[] args)
4444

4545
private static async Task HandleFileActionAsync(string[] newPaths)
4646
{
47-
// Add new paths to the temporary file
48-
newPaths.SafelyWriteLinesToFile(Program.TempFilePath, false);
47+
// Ensure the temporary directory exists
48+
Directory.CreateDirectory(Program.TempFilePath);
49+
50+
// Add new paths to a temporary file
51+
string randomFile = Path.Combine(Program.TempFilePath, Path.GetRandomFileName());
52+
await File.AppendAllLinesAsync(randomFile, newPaths);
4953

5054
using Mutex mutex = new(true, Program.MutexName, out bool createdNew);
5155

@@ -57,24 +61,28 @@ private static async Task HandleFileActionAsync(string[] newPaths)
5761
// Waiting if more files/folders are selected
5862
await Task.Delay(Program.BatchTimeoutMs);
5963

60-
int previousCount, nextCount = (await File.ReadAllLinesAsync(Program.TempFilePath)).Length;
64+
int previousCount, nextCount = Directory.GetFiles(Program.TempFilePath).Length;
6165

6266
// Wait until the number stops increasing
6367
do
6468
{
6569
previousCount = nextCount;
6670
await Task.Delay(Program.BatchTimeoutMs);
67-
nextCount = (await File.ReadAllLinesAsync(Program.TempFilePath)).Length;
71+
nextCount = Directory.GetFiles(Program.TempFilePath).Length;
6872
}
6973
while (previousCount != nextCount);
7074

7175
List<string> allPaths = new();
7276

73-
// Get all paths from the temporary file
74-
if (File.Exists(Program.TempFilePath))
77+
// Get all paths from the temporary files
78+
if (Directory.Exists(Program.TempFilePath))
7579
{
76-
allPaths = (await File.ReadAllLinesAsync(Program.TempFilePath)).ToList();
77-
File.Delete(Program.TempFilePath);
80+
foreach (var file in Directory.GetFiles(Program.TempFilePath))
81+
{
82+
allPaths.AddRange(await File.ReadAllLinesAsync(file));
83+
}
84+
85+
Directory.Delete(Program.TempFilePath, true);
7886
}
7987

8088
int count = allPaths.Count;

src/ContextMenuUploader/RegistryHelper.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public static void AddContextMenu(string appName, string appPath, string context
2020
string regFolderPath = @"Directory\shell\" + appName;
2121
RegistryHelper.CreateRegistryKey(regFolderPath, appPath, contextMenuLabel);
2222

23-
// Set the MultipleInvokePromptMinimum value to 256
24-
RegistryHelper.SetMultipleInvokeLimit(256);
23+
// Set the MultipleInvokePromptMinimum value to 512
24+
RegistryHelper.SetMultipleInvokeLimit(512);
2525

26-
Console.WriteLine("Context menu option added for up to 256 files and folders.");
26+
Console.WriteLine("Context menu option added for up to 512 files and folders.");
2727
}
2828
catch (Exception ex)
2929
{
@@ -43,8 +43,8 @@ public static void RemoveContextMenu(string appName)
4343
string regFolderPath = @"Directory\shell\" + appName;
4444
Registry.ClassesRoot.DeleteSubKeyTree(regFolderPath, false);
4545

46-
// Reset the MultipleInvokePromptMinimum value to 15
47-
RegistryHelper.SetMultipleInvokeLimit(15);
46+
// Reset the MultipleInvokePromptMinimum value
47+
RegistryHelper.SetMultipleInvokeLimit();
4848

4949
Console.WriteLine("Context menu option removed for files and folders.");
5050
}
@@ -54,17 +54,27 @@ public static void RemoveContextMenu(string appName)
5454
}
5555
}
5656

57-
public static void SetMultipleInvokeLimit(int limit)
57+
public static void SetMultipleInvokeLimit(int? limit = null)
5858
{
5959
try
6060
{
6161
// Open the registry key
6262
using RegistryKey? key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer", true);
6363

64-
// Set the "MultipleInvokePromptMinimum" value to the provided limit
65-
key?.SetValue("MultipleInvokePromptMinimum", limit, RegistryValueKind.DWord);
64+
if (limit.HasValue)
65+
{
66+
// Set the "MultipleInvokePromptMinimum" value to the provided limit
67+
key?.SetValue("MultipleInvokePromptMinimum", limit, RegistryValueKind.DWord);
6668

67-
Console.WriteLine($"Successfully set MultipleInvokePromptMinimum to {limit}");
69+
Console.WriteLine($"Successfully set MultipleInvokePromptMinimum to {limit}");
70+
}
71+
else
72+
{
73+
// Reset the "MultipleInvokePromptMinimum"
74+
key?.DeleteValue("MultipleInvokePromptMinimum");
75+
76+
Console.WriteLine($"Successfully reset MultipleInvokePromptMinimum");
77+
}
6878
}
6979
catch (Exception ex)
7080
{

src/ContextMenuUploader/StringExtensions.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)