Skip to content

Commit b8cf7fe

Browse files
authored
Fix multiple fulltrust process instances running (#889)
1 parent 0adf42e commit b8cf7fe

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Files.Launcher/Program.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ private static void Main(string[] args)
3535

3636
// Only one instance of the fulltrust process allowed
3737
// This happens if multiple instances of the UWP app are launched
38-
var mutex = new Mutex(true, "FilesUwpFullTrust", out bool isNew);
38+
using var mutex = new Mutex(true, "FilesUwpFullTrust", out bool isNew);
3939
if (!isNew) return;
4040

41-
// Create shell COM object and get recycle bin folder
42-
recycler = new ShellFolder(Vanara.PInvoke.Shell32.KNOWNFOLDERID.FOLDERID_RecycleBinFolder);
43-
Windows.Storage.ApplicationData.Current.LocalSettings.Values["RecycleBin_Title"] = recycler.Name;
44-
45-
// Create shell watcher to monitor recycle bin folder
46-
var watcher = new ShellItemChangeWatcher(recycler, false);
47-
watcher.NotifyFilter = ChangeFilters.AllDiskEvents;
48-
watcher.Changed += Watcher_Changed;
49-
//watcher.EnableRaisingEvents = true; // TODO: uncomment this when updated library is released
50-
5141
try
5242
{
43+
// Create shell COM object and get recycle bin folder
44+
recycler = new ShellFolder(Vanara.PInvoke.Shell32.KNOWNFOLDERID.FOLDERID_RecycleBinFolder);
45+
Windows.Storage.ApplicationData.Current.LocalSettings.Values["RecycleBin_Title"] = recycler.Name;
46+
47+
// Create shell watcher to monitor recycle bin folder
48+
watcher = new ShellItemChangeWatcher(recycler, false);
49+
watcher.NotifyFilter = ChangeFilters.AllDiskEvents;
50+
watcher.Changed += Watcher_Changed;
51+
//watcher.EnableRaisingEvents = true; // TODO: uncomment this when updated library is released
52+
5353
// Connect to app service and wait until the connection gets closed
5454
appServiceExit = new AutoResetEvent(false);
5555
InitializeAppServiceConnection();
@@ -60,6 +60,8 @@ private static void Main(string[] args)
6060
connection?.Dispose();
6161
watcher?.Dispose();
6262
recycler?.Dispose();
63+
appServiceExit?.Dispose();
64+
mutex?.ReleaseMutex();
6365
}
6466
}
6567

@@ -113,6 +115,7 @@ private static async void Watcher_Changed(object sender, ShellItemChangeWatcher.
113115
private static AppServiceConnection connection;
114116
private static AutoResetEvent appServiceExit;
115117
private static ShellFolder recycler;
118+
private static ShellItemChangeWatcher watcher;
116119

117120
private static async void InitializeAppServiceConnection()
118121
{

0 commit comments

Comments
 (0)