-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add an example to setControlCHook #19416
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2320,6 +2320,15 @@ when notJSnotNims: | |
proc setControlCHook*(hook: proc () {.noconv.}) | ||
## Allows you to override the behaviour of your application when CTRL+C | ||
## is pressed. Only one such hook is supported. | ||
## Example: | ||
## | ||
## .. code-block:: Nim | ||
## proc ctrlc() {.noconv.} = | ||
## echo "Ctrl+C fired!" | ||
## # do clean up stuff | ||
## quit() | ||
## | ||
## setControlCHook(ctrlc) | ||
Comment on lines
+2323
to
+2331
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the future: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you - I was advised that in this case it would be tricky to send a ctrl-c for testing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you once again xflywind. I don't know what "But runnableExamples seems not to be used for forward declare" means? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It simply doesn't work for forward declare. proc setControlCHook*(hook: proc () {.noconv.})
## Allows you to override the behaviour of your application when CTRL+C
## is pressed. Only one such hook is supported.
runnableExamples("-r:off"):
proc ctrlc() {.noconv.} =
echo "Ctrl+C fired!"
# do clean up stuff
quit()
setControlCHook(ctrlc) => Error: invalid indentation |
||
|
||
when not defined(noSignalHandler) and not defined(useNimRtl): | ||
proc unsetControlCHook*() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this example is broken: ctrl-c hooks are signal handlers, and you can't (reliably) allocate memory inside them, which
echo
does - this is a common pitfall with using signal handlersThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a new to Nim and I don't understand a lot, and when I wanted to catch Control-C I looked in system, and there was no example, but luckily I found one in stack overflow for Nim, otherwise I would have been stuck. It was suggested I make a PR to add the example, and this is the first PR I've make (and I had lots of hand-holding to do it from the Nim channel, to guide me step-by-step though the process), and so with that in mind, I'm confused...
I can do:
... and it does work?