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: 1241 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 
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 Nov 04 15:01:36 CET 2025 
 Total time taken to generate the page: 0.04667 seconds 
 |