Squirrel version(s)
1.9.1
I migrated a Wpf app to dot.net core 3 and use squirrel to distribute it.
I am getting a strange behavior with the part of the code that checks if another instance of the app is running, and if so will not allow another one to run:
// Get Reference to the current Process
var thisProc = Process.GetCurrentProcess();
// Check how many total processes have the same name as the current one
if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
{
// If there is more than one, then it is already running.
MessageBox.Show("The application is already running.", "Application already running", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(1);
}
else
{
UpdateUserStatusInDb();
DisplayRootViewFor<ShellViewModel>();
}
For some reason after the first start and close of the app I am unable to start it again since it seems that another process is already running, but I cannot find it in Process Explorer.
Is Squirrel running a background process with the same name as the app?
This issue is not seen if I am running the app in the debug mode or release mode, it only appears after running the releasified version of the app which got me thinking it must have something to do with Squirrel.
Digging further I got the following situation:
The app is installed by Squirrel in AppDataLocalSdeHelper, if I am running the executable found in the above directory I am getting my MessageBox.Show("The application is already running.", "Application already running", MessageBoxButton.OK, MessageBoxImage.Error); and then the app closes.
But if I am running the app from AppDataLocalSdeHelperapp-3.1.3 I don't get this issue.
I believe this is due to that stub running first, and then invoking your process. Since they both have the same name, this is expected.
A quick and dirty way of doing this would be to get the full path of the current process, and then to check the full paths of the processes GetProcessesByName returns.
You should be able to find out paths via the MainModule property or something similar on the processes. (If my memory isn't failing me.)
However, this is still pretty much a dirty hack, and it wouldn't help with preventing the user from launching different versions at the same time. Ideally you should use a better approach like a named system mutex. It should be available on System.Threading.Mutex.OpenExistingMutex or something along those lines.
Edit: Just checked, and apparently it should be: System.Threading.Mutex.OpenExisting
Yep, I believe that @uygary has diagnosed this correctly. I would use @onovotny's SingleInstanceHelper library instead - https://github.com/onovotny/SingleInstanceHelper. Also, when you use this library, you must make 100% sure that Squirrel Events are processed _outside_ of this library (i.e. handle Squirrel Events before you initialize single instance), or else you will completely break your update path
@anaisbetts I do need to write up docs, though the samples should help. A sample/doc specific to Squirrel would be neat too.
Most helpful comment
Yep, I believe that @uygary has diagnosed this correctly. I would use @onovotny's SingleInstanceHelper library instead - https://github.com/onovotny/SingleInstanceHelper. Also, when you use this library, you must make 100% sure that Squirrel Events are processed _outside_ of this library (i.e. handle Squirrel Events before you initialize single instance), or else you will completely break your update path