Determine if only one instance is running: Difference between revisions
Content added Content deleted
m (Fixed formatting.) |
m (Added "more proper"(?) method) |
||
Line 12: | Line 12: | ||
'''Language Version:''' 2.5 |
'''Language Version:''' 2.5 |
||
===without `inspect`=== |
|||
⚫ | |||
<pre> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
frame = sys._getframe() |
frame = sys._getframe() |
||
while frame.f_back: |
while frame.f_back: |
||
frame = frame.f_back |
frame = frame.f_back |
||
if not os.system('(( $(ps - |
if not os.system('(( $(ps -aef | grep \'[' + frame.f_code.co_filename[0] + \ |
||
']' + frame.f_code.co_filename[1:len(frame.f_code.co_filename)] + '\' | wc \ |
']' + frame.f_code.co_filename[1:len(frame.f_code.co_filename)] + '\' | wc \ |
||
-l) > 1 ))'): |
-l) > 1 ))'): |
||
return 0 |
return 0 |
||
else: |
else: |
||
return 1 |
return 1 |
||
</pre> |
|||
===with `inspect`=== |
|||
<pre> |
|||
import inspect, os |
|||
def isOnlyInstance() |
|||
frame = inspect.currentframe() |
|||
while frame.f_back: |
|||
frame = frame.f_back |
|||
if not os.system('(( $(ps -aef | grep \'[' + frame.f_code.co_filename[0] + \ |
|||
']' + frame.f_code.co_filename[1:len(frame.f_code.co_filename)] + '\' | wc \ |
|||
-l) > 1 ))'): |
|||
return 0 |
|||
else: |
|||
return 1 |
|||
</pre> |
Revision as of 04:45, 9 December 2007
Determine if only one instance is running
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
This task is to determine if there is only one instance of an application running.
Visual Basic
Language Version: 6
Dim onlyInstance as Boolean onlyInstance = not App.PrevInstance
Python
Language Version: 2.5
without `inspect`
import sys, os def isOnlyInstance(): frame = sys._getframe() while frame.f_back: frame = frame.f_back if not os.system('(( $(ps -aef | grep \'[' + frame.f_code.co_filename[0] + \ ']' + frame.f_code.co_filename[1:len(frame.f_code.co_filename)] + '\' | wc \ -l) > 1 ))'): return 0 else: return 1
with `inspect`
import inspect, os def isOnlyInstance() frame = inspect.currentframe() while frame.f_back: frame = frame.f_back if not os.system('(( $(ps -aef | grep \'[' + frame.f_code.co_filename[0] + \ ']' + frame.f_code.co_filename[1:len(frame.f_code.co_filename)] + '\' | wc \ -l) > 1 ))'): return 0 else: return 1