Home » Community » Newbie corner » Stopping ReadStdIn() function
Re: Stopping ReadStdIn() function [message #57043 is a reply to message #57030] |
Mon, 17 May 2021 11:50 |
Oblivion
Messages: 1094 Registered: August 2007
|
Senior Contributor |
|
|
You can also write a non-blocking version of ReadStdIn so that you can set a timeout value. (this is for posix, but it can be easily adapted to windows)
void SetNonBlocking()
{
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
}
void SetBlocking()
{
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) & ~O_NONBLOCK);
}
String ReadStdIn2(int timeout)
{
String r;
SetNonBlocking();
int t = msecs();
do {
SocketWaitEvent we;
we.Add(STDIN_FILENO, WAIT_READ);
if(we.Wait(10) && (we[0] & WAIT_READ)) {
int c = 0;
int n = read(STDIN_FILENO, (char*) &c, 1);
if(c == '\n')
break;
if(n > 0) {
r.Cat(c);
t = msecs();
}
}
}
while(msecs(t) < timeout && !done);
SetBlocking();
return r.GetCount() ? r : String::GetVoid();
}
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Mon, 17 May 2021 13:01] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Tue May 14 10:09:24 CEST 2024
Total time taken to generate the page: 0.02335 seconds
|