Silverfrost Forums

Welcome to our forums

How to check if a program is running

14 Mar 2016 10:33 #17296

I need to detect if another program is running or not from within a F95 application.

From within a F95 program (P1) I start a second process (P2) and continue P1. (I use Start_PProcess to start P2). Before P1 starts P2 a second time P1 must wait until P2 finished.

Any idea how to do that?

Thanks in advance.

14 Mar 2016 9:38 #17300

There will be a good way to do this but if you are happy with a cheap and nasty approach then just create a file in the process that is terminating and check if the file has been created in the process that is waiting. But while waiting make sure that you don't use up the cpu by calling sleep1@ in the waiting loop.

A more refined way is to use START_THREAD@ and WAIT_FOR_THREAD@. These are defined in cwplus.enh, the clearwin+ enhancements file.

There are other ways that may be better still.

17 Mar 2016 1:10 #17309

Assume your 2 processes were both written in FTN95.

In the main window creation for P2, set it's process name and a callback function thus:

       IA=WINIO@('%nc[P2_process_name]%rm&',MESSAGE_FN)

The code to launch P2 needs to look something like:

       INTEGER FUNCTION LaunchP2_FN()
C      -------------------------------
C
C      Launch P2.exe
C
C      -----------------------------------------------------------------
       INTEGER START_PPROCESS@, SEND_TEXT_MESSAGE@
       CHARACTER*(25) REPLY
       INCLUDE <WINDOWS.INS>
C     ------------------------------------------------------------------

       IA = SEND_TEXT_MESSAGE@('P2_process_name','RUNNING',REPLY)

       IF (REPLY .NE. 'YES') THEN
       IA = START_PPROCESS@('P2.EXE',' ')
       IF (IA .NE. 0) write(*,*) 'Did not start P2'
       ENDIF

       LaunchP2_FN = 1
       END

The callback in P2 looks like this:

       INTEGER FUNCTION MESSAGE_FN()
C      -----------------------------
C
       CHARACTER*(255) MESSAGE
       INCLUDE <WINDOWS.INS>

       MESSAGE = CLEARWIN_STRING@('MESSAGE_TEXT')
       IF (MESSAGE .EQ. 'CLOSE') THEN
           MESSAGE_FN = 0
           RETURN
       ELSE IF (MESSAGE .EQ. 'RUNNING') THEN
           CALL REPLY_TO_TEXT_MESSAGE@('YES')
       ENDIF

       MESSAGE_FN = 1
       RETURN
       END

Essentially, P1 sends the message 'RUNNING' to P2. If there is no reply, then P2 can be started. If there is an answer, it will be 'YES', in which case do not start P2 at all, because it is already running. P1 can also send the message 'CLOSE' (using SEND_TEXT_MESSAGE@), in which case P2 should shut itself down. In that case, the reply specified in the SEND_TEXT_MESSAGE@ call might just as well be ignored.

Be sure to read all about Windows messaging in FTN95.CHM.

Eddie

Please login to reply.