How to check if a Task is already running?
I want to check, if my program has been started before to prevent another instance by double clicking the icon again by mistake.
I cannot use FindTask() as it will always find itself, so I cannot find the other task that MAY be started before. |
AttemptSemaphore?
|
Check if program message port is open or not maybe?
|
I check with
jsr (_LVOFindPort,a6)if there is already a named port, possibly added with a precedent jsr (_LVOAddPort,a6). There are probably better methods but this works, in every kickstart, and is very simple. |
Another possibility is to Lock() your program's executable with exclusive access mode straight after start.
This way you get a meaningful error message via IoErr() (202: object is busy), if it's opened again. However, there is a caveat, if your program crashes, the lock might remain open forever, so prepare special cleanup code for this. The FindPort solution is nice, too, but you should be careful about name collisions with other applications running. And beware of race conditions occuring by both methods! Always sempahore protect such code! |
Quote:
|
Ia the port introduces race conditions, and has to be protected with a semaphore, then wouldn't it make more sense to use AttemptSemaphore already mentioned by deimos?
After all you will need a semaphore anyway, and this is a exactly the purpose this is for. :) |
All times are GMT +2. The time now is 18:39. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.