-
December 29th, 2022, 04:00 AM
#1
Is there a better way to check if an app is already running ?
I work on a cross-platform app, called Mixbus, which checks (at startup) in case there's already a running instance. Here's the code we use in Windows:-
Code:
#ifdef PLATFORM_WINDOWS
CreateMutexA (0, 1, string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str ());
if (GetLastError() == ERROR_ALREADY_EXISTS) {
Gtk::Main main (argc, argv);
Gtk::MessageDialog msg (string_compose (_("%1 is already running."), PROGRAM_NAME),
false, Gtk::MESSAGE_ERROR , Gtk::BUTTONS_OK, true);
msg.run ();
exit (EXIT_FAILURE);
}
#endif
Is CreateMutex a good way to test this?? It does correctly display the error message if it detects the app already running but one user's been complaining that he often sees a message box saying Mixbus is already running - even when it isn't already running. I can't reproduce it on my system but I'm wondering if the code string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str () is sometimes returning NULL? If I understand the MSDN documentation, this would result in the mutex being unnamed (and ERROR_ALREADY_EXISTS only gets returned for a named mutex IIUC). So maybe we should be carrying out some initialisation using SetLastError()? Or maybe there's a better way to check, rather than using CreateMutexA() in the first place?
Last edited by John E; December 29th, 2022 at 04:39 AM.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 29th, 2022, 05:15 AM
#2
Re: Is there a better way to check if an app is already running ?
I'm wondering if the code string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str () is sometimes returning NULL
What does string_compose do? What are PROGRAM_NAME and PROGRAM_VERSION defined as?
In general, yes, CreateMutex is a good way of testing for multiple running instances. It's what we use without any issues AFAIK.
From our code:
Code:
int main() {
if (const auto mtxevt {CreateMutex(NULL, TRUE, unqhpevt)}; GetLastError() == ERROR_SUCCESS) {
// Created OK so only this running
try {
// Code to process goes here - we just call the main processing function
} catch (...) {
std::cout << "Unhandled exception\n";
}
ReleaseMutex(mtxevt);
CloseHandle(mtxevt);
} else {
// Error - already running
std::cout << "Program already running!\n";
}
}
Note that we check for success - not check for failure.
Last edited by 2kaud; December 29th, 2022 at 05:26 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.2)
-
December 29th, 2022, 05:16 AM
#3
Re: Is there a better way to check if an app is already running ?
AFAIK using the mutex was (and I believe is) the best way to check if an app is already running.
There are howwever some "features" conserning what means "app is already running"; in the same session, user account, in the system, ...
The full concept was discussed a lot of times. Some conclusions with the sample code you will find on the Joe Newcomer site: Avoiding Multiple Instances of an Application
Last edited by VictorN; December 29th, 2022 at 11:24 AM.
Victor Nijegorodov
-
December 29th, 2022, 05:17 AM
#4
Re: Is there a better way to check if an app is already running ?
Some things to try.
Get your user to try process explorer from https://learn.microsoft.com/en-gb/sysinternals/
It should be possible to search for the name of the mutex.
Also, it can check whether the previous instance of Mixbus has truly gone, or some runt process remains.
Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
If it didn't completely exit, then the mutex would remain.
> but I'm wondering if the code string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str () is sometimes returning NULL?
One way to check is to store the result in a variable, and also print the name as part of the error message.
-
December 29th, 2022, 06:43 AM
#5
Re: Is there a better way to check if an app is already running ?
Many thanks guys. I'm afraid CodeGuru isn't sending me notifications today but anyway...
string_compose() is a function which accepts 2 parameters (either char* or int) and returns std::string. So if PROGRAM_NAME is Mixbus and PROGRAM_VERSION is 19 it'll return Mixbus19
I deliberately tried making it return an empty string (i.e. so that CreateMutexA() would generate an unnamed mutex) and as expected, GetLastError() then returns zero.
Having said all that... I'd no idea that the term "multiple instance" could have different meanings!! I'll do a bit more reading.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 30th, 2022, 07:05 AM
#6
Re: Is there a better way to check if an app is already running ?
 Originally Posted by John E
I'm afraid CodeGuru isn't sending me notifications today
Whaaa? I just got a notification for this post which got submitted over 6 weeks ago!!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 30th, 2022, 07:53 AM
#7
Re: Is there a better way to check if an app is already running ?
It's a known issue....
From Steve Jones (admin) 12 Nov 2022:
OH BOY....I just now got an email TWELVE DAYS LATE.... Anyway, the boss said it might be a while before they can get someone to fix the issue(s).
Last edited by 2kaud; December 30th, 2022 at 08:24 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.2)
-
December 30th, 2022, 09:27 AM
#8
Re: Is there a better way to check if an app is already running ?
 Originally Posted by John E
Whaaa? I just got a notification for this post which got submitted over 6 weeks ago!! 
Yes, this CG Bug is already some months old. And it will be probably fixed in the future... 
Victor Nijegorodov
-
May 9th, 2023, 04:07 AM
#9
Re: Is there a better way to check if an app is already running ?
 Originally Posted by VictorN
 Originally Posted by John E
Whaaa? I just got a notification for this post which got submitted over 6 weeks ago!! 
Yes, this CG Bug is already some months old. And it will be probably fixed in the future...  
Not fixed yet unfortunately... I just got a notification for 2kaud's post from 5 months ago !!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
May 9th, 2023, 06:04 AM
#10
Re: Is there a better way to check if an app is already running ?
Everything comes to those that wait...
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.2)
-
May 9th, 2023, 11:04 AM
#11
-
May 9th, 2023, 11:09 AM
#12
Re: Is there a better way to check if an app is already running ?
However, I got the notification of this your post today!!!
Huh - no such luck here. Neither for John's nor Victor's post.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.2)
-
May 14th, 2023, 04:54 AM
#13
Re: Is there a better way to check if an app is already running ?
I'm surprised the site owners aren't clamouring to get this fixed. It's been a problem now for a year or more and must've had a devastating affect on site usage...
"A problem well stated is a problem half solved.” - Charles F. Kettering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|