Recommended way to handle keypresses in my script? #299
-
|
I have a script that runs what is currently an infinite loop that I have to stop execution to break out of, but this doesn't allow for a clean exit. In console apps, I'm accustomed to acheiving an interruptable loop using Console.KeyAvailable to check for keyboard input, so I can break out with a key press. However, this doesn't work in a NetPad script (apparently because stdin/out are redirected). Is there a recommended way to handle keyboard input in a NetPad script? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Not as when dealing with a terminal directly. You can't interact with the script process directly using key input. The only time you can pass it input is when you do To stop a script abruptly you can use the stop button but if you're looking to use input to stop your loop gracefully you can have a |
Beta Was this translation helpful? Give feedback.
-
|
Running Console.ReadLine() in a separate thread worked perfectly, thank you! |
Beta Was this translation helpful? Give feedback.
Not as when dealing with a terminal directly. You can't interact with the script process directly using key input. The only time you can pass it input is when you do
Console.ReadLine()and NetPad will give you a place to type your input and it passes it along to the script.To stop a script abruptly you can use the stop button but if you're looking to use input to stop your loop gracefully you can have a
Console.ReadLine()running in a separate thread in your code and when you type 'stop' or something it will set a flag that your infinite loop is checking on every iteration and exits if the flag is set. I haven't tried that scenario within NetPad but I can't think of why it wouldn't work.…