Input timeout

Ali Çehreli acehreli at yahoo.com
Mon May 13 21:14:27 PDT 2013


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



More information about the Digitalmars-d-learn mailing list