Input timeout
Josh
moonburntm at gmail.com
Mon May 13 21:22:56 PDT 2013
On Tuesday, 14 May 2013 at 04:14:27 UTC, Ali Çehreli wrote:
> On 05/13/2013 08:50 PM, Josh wrote:
>> Is there a way in D to only accept input for a certain time,
>> instead of
>> std.stdio.readln's behaviour? Something like "Press a key in 3
>> seconds
>> to abort".
>>
>> Thanks
>>
>> Josh
>
> An unlikely solution is std.concurrency because it already has
> a timeout facility:
>
> import std.stdio;
> import std.concurrency;
> import std.datetime;
> import std.string;
>
> void lineReader(Tid owner)
> {
> while (true) {
> string line = readln().chomp();
> owner.send(line);
> }
> }
>
> void main()
> {
> spawn(&lineReader, thisTid);
>
> while (true) {
> auto received =
> receiveTimeout(3.seconds,
> (string line) {
> writefln("Thanks for -->%s<--",
> line);
> });
>
> if (!received) {
> writeln("Patiently waiting...");
> }
> }
> }
>
> Ali
Thanks Ali, that's almost what I need. Your answer requires the
user to press enter to send the line. Would it be possible to
have getch-like behaviour without using C, or would that be the
only way?
More information about the Digitalmars-d-learn
mailing list