Link to home
Start Free TrialLog in
Avatar of Michel021497
Michel021497

asked on

Detecting of clicking on STOP-button

Is there a way I can check if an user hits the STOP-button or the BACK-button?
I want to know this because my program crashes if someone hits those buttons while running.
Thus I must create a routine that starts when hitting one of the buttons.
Avatar of faster
faster

What is your "program"?  if it is JAVA applet, then some of its methods will be invoked by the browser.
Avatar of Michel021497

ASKER

It's a C program. So no JAVA.
Edited text of question
Avatar of ozo
Where does your C program crash?
Well, yours is a c program, then where does it run?  Is it a CGI that runs on the server (unlikely) or a browser plugin (also unlikely) or an activex control?
The program writes in a file. Because it's not posible that 2 persons write in one file at the same time, I made a lock on the file.
 But if the STOP button is hit, the file is never unlocked.
So no one is able to use the program anymore after such action.
Can you put a timeout on the lock?
Can you limit the lock window to times when the user is not upoading data?
You didn't answer my question, is it a CGI program or an activex control?
It's a CGI program.

ASKER CERTIFIED SOLUTION
Avatar of dmethvin
dmethvin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, I thought flock or lockf would release the lock when the process teminates.
No, no and no! This is not the answer, i'm afraid.

What happens after the client closes the connection is not defined by the CGI protocol because it has nothing to do with CGI; it has to do with handling socket connections.

And, you want get any SIGPIPE on any connection side, unless you _write_ to the socket.

Here is a scheme to handle the client closing the connection:

1. catch SIGALRM;
2. catch SIGPIPE; (handling EPIPE is another option)

3. setup a timer which interrupts your CGI process every N seconds;

4. in the SIGALRM handler, write - say - a new-line char to the socket; if the connection is closed, you'll get a SIGPIPE;

5. in the SIGPIPE handler, handle cleanup, because the socket has died.

The socket FAQ pages are the best reference on this kind of stuff (sorry, cannot remember the address).

Rgds, julio
I think I said the same thing, didn't I? Stdout is mapped to a socket. You'll get a SIGPIPE if you write to stdout. The alarm isn't needed unless you're doing long stretches with no output and want to detect the stop, but it does generalize the solution nicely. (Never mind my lock comment, I was confusing this with another message.)