Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-Blocking read from standard I/O in C# [closed]

I want a non-blocking read function from console. How do I write that in C#?

like image 582
Masoud Avatar asked Apr 11 '11 11:04

Masoud


People also ask

What is non-blocking IO in C?

Sometimes it's convenient to have I/O that doesn't block i.e we don't want a read call to block on one in case of input from the other. Solution for this is the given function: To specify non-blocking option: #include<fcntl. h> int fd; fcntl(fd, F_SETFL, O_NONBLOCK);

Does read () block in C?

In blocking mode read() will block; in non-blocking mode if there is no data it will return -1 with errno set to EAGAIN or EWOULDBLOCK depending in your platform.

What are available for non-blocking IO operations?

Non blocking I/O Java NIO non- blocking mode allows the thread to request writing data to a channel, but not wait for it to be fully written. The thread is allowed to go on and do something else in a mean time.

What is blocking and non-blocking in input and output?

Blocking refers to operations that block further execution until that operation finishes while non-blocking refers to code that doesn't block execution. Or as Node. js docs puts it, blocking is when the execution of additional JavaScript in the Node. js process must wait until a non-JavaScript operation completes.


1 Answers

Richard Dutton has a solution on his blog:

while (true)   {       if (Console.KeyAvailable)       {           ConsoleKeyInfo key = Console.ReadKey(true);           switch (key.Key)           {               case ConsoleKey.F1:                   Console.WriteLine("You pressed F1!");                   break;               default:                   break;           }       }       // Do something more useful   }  
like image 200
odrm Avatar answered Sep 19 '22 10:09

odrm