English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. System (https://eab.abime.net/forumdisplay.php?f=113)
-   -   How to check if a Task is already running? (https://eab.abime.net/showthread.php?t=100035)

Honitos 16 December 2019 09:41

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.

deimos 16 December 2019 09:58

AttemptSemaphore?

daxb 16 December 2019 11:24

Check if program message port is open or not maybe?

ross 16 December 2019 11:47

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.

BastyCDGS 23 December 2019 08:22

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!

Wilsom1992 28 February 2020 10:30

Quote:

Originally Posted by BastyCDGS (Post 1366946)
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!

wanted to advice the same.

sparhawk 29 February 2020 21:05

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.

Page generated in 0.04161 seconds with 11 queries