jackxiao / jslibs

Automatically exported from code.google.com/p/jslibs
0 stars 0 forks source link

Event or Exeception handling posibility for Ctrl+C #71

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
For handling a Ctrl+C we have endSignal, so this is possible:

{{{
while( !endSignal ) {
  ... do stuff ...
}
0; // Exited
}}}

However, this does not work well for blocking functions.
{{{
while( !endSignal ) {
  Print( File.stdin.Read() );
}
}}}

You can't exit that unless you use Ctrl+C, and then free the blocking call.
In this case you have to hit Crtl+C plus enter, in the case of Sockets
you'll have to end the task externally and Ctrl+C won't work like it's
supposed to.

In other languages like Python, instead of a variable they have a
KeyboardInterupt exception and that gets caught.

I'm not saying to just add in an Exception. There is a downside to that
making Ctrl+C abort in places we may not expect it to.

I attempted to put a watch on endSignal, but failed.
The best method of doing this, is probably something like a way to define
an event that gets triggered by endSignal, and then we can throw our own
Exception from there when we know we are going to be using a Blocking call
a few lines below.

{{{
while(true) {
  try {
    ...define some event here to throw a KeyboardInterupt...
    ...do some blocking code...
  } catch ( e if e instanceof KeyboardInterupt ) break;
}
}}}

Original issue reported on code.google.com by nadir.se...@gmail.com on 12 Nov 2008 at 12:03

GoogleCodeExporter commented 9 years ago
I understand your need and the behavior you describe is quite interesting but
implementing such functionality will force me to enable threadsafe for 
SpiderMonkey
and jslibs. SpiderMonkey already has threadsafe facility but jslibs not.
Managing ctrl-c with socket is not an issue and cab be done like this:
 while(!endSignal) {
  Poll( <socket descriptor list> , <timeout>);
 }
If you set a small timeout (like 250 millisecond or less), the ctrl-c will be 
taken
in account quite quickly without any penalty for your application.

Original comment by sou...@gmail.com on 14 Nov 2008 at 6:17

GoogleCodeExporter commented 9 years ago
Note that I accept this issue (with a low priority) because if I will have need 
to
manage thread safety for jslibs, this issue will be fixed easily.

Original comment by sou...@gmail.com on 14 Nov 2008 at 6:37

GoogleCodeExporter commented 9 years ago
see ProcessEvents(Descriptor.Events(descList), EndSignalEvents(), 
TimeoutEvents(100)); ...

Original comment by sou...@gmail.com on 2 Jun 2011 at 10:54