Difference between revisions of "Preventing two instances of a program to run simultaneously"
From wiki.visual-prolog.com
(Copied from "Tips, How To's, Code Samples, etc") |
m (categorized) |
||
Line 20: | Line 20: | ||
''If you are using a named mutex to limit your application to a single instance, a malicious user can create this mutex before you do and prevent your application from starting. To prevent this situation, create a randomly named mutex and store the name so that it can only be obtained by an authorized user. Alternatively, you can use a file for this purpose. To limit your application to one instance per user, create a locked file in the user's profile directory.'' | ''If you are using a named mutex to limit your application to a single instance, a malicious user can create this mutex before you do and prevent your application from starting. To prevent this situation, create a randomly named mutex and store the name so that it can only be obtained by an authorized user. Alternatively, you can use a file for this purpose. To limit your application to one instance per user, create a locked file in the user's profile directory.'' | ||
[[Category:Tips and Tricks]] | |||
[[category:Multithreading]] |
Revision as of 16:01, 23 September 2007
You can use this code to prevent a program from being started again when it already runs:
class facts okToRunMutex : mutex := erroneous. class predicates okToRun : () determ. clauses okToRun() :- okToRunMutex := mutex::createNamed("SomeStrangeName", false()), not(multiThread_native::wait_timeout = okToRunMutex:wait(0)). clauses run():- if okToRun() then TaskWindow = taskWindow::new(), TaskWindow:show() end if.
You are supposed to use some strange name instead of "SomeStrangeName". Microsoft has this to say about the subject:
If you are using a named mutex to limit your application to a single instance, a malicious user can create this mutex before you do and prevent your application from starting. To prevent this situation, create a randomly named mutex and store the name so that it can only be obtained by an authorized user. Alternatively, you can use a file for this purpose. To limit your application to one instance per user, create a locked file in the user's profile directory.