473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

setting timeout for Console.readline()

Hi,

I've a question on setting timeout on console.readline()

I would like the user to input something through Console.readline() in
5 secs. If there is no input in that time, I would like to proceed
further with the program logic.

I had a difficulty in implementing this, as console.readline()
indefinitely waits for the user input.

Can somebody help me to implement this in c#?

Thanks,
Srinivas

Apr 2 '07 #1
2 14299
On Sun, 01 Apr 2007 23:20:29 -0700, SriBhargav <sr************@gmail.com>
wrote:
[...]
I would like the user to input something through Console.readline() in
5 secs. If there is no input in that time, I would like to proceed
further with the program logic.

I had a difficulty in implementing this, as console.readline()
indefinitely waits for the user input.

Can somebody help me to implement this in c#?
I've got to say, your question provided me with the opportunity for a
little chuckle. Not because of your post per se, but I decided to browse
the docs a little in the Console class and I found a pretty funny pair of
document pages:

http://msdn2.microsoft.com/en-us/lib....readline.aspx
http://msdn2.microsoft.com/en-us/lib...sole.read.aspx

In the former (the docs for Console.ReadLine), they recommend using
Console.Read so as to "produce robust code". In the latter (the docs for
Console.Read), they claim that Console.ReadLine is preferable to using
Console.Read. It's like the two methods were documented by different
writers, and neither really wants "their" method to be used.

I hope no one at Microsoft wonders why it is people sometimes get confused
trying to figure out .NET. :)

Anyway, as far as the actual question goes, it seems to me that there are
at least a few possibilities:

* Use Console.KeyAvailable and Console.ReadKey together to check for
input. Since you'd have to poll the Console in order to avoid making the
user wait too long when they DO provide input, this seems to me the least
desirable solution. However, in a console application where you're
waiting for input and aren't doing anything else anyway, I don't suppose
it would hurt anything to just check for user input every quarter second
or so (obviously you don't want to just poll without any sort of delay
between checks, and I think a quarter second would probably be sufficient
to allow your application to appear to be responsive).

* Get the input stream for Console and then set the ReadTimeout
property of that stream. Note that I don't actually know for sure that
the default input stream for Console actually supports the ReadTimeout
property. You'll have to check that yourself. But if it does, perhaps
that would do what you want.

* Read the input on a separate thread (using Read or ReadLine) and
maintain a timer that aborts the thread if no user input has been provided
by a certain time. Yes, it involves aborting the thread and personally I
don't like doing that. However, as far as I know there's no mechanism to
cause the Console input method to be interrupted, and so the only option
appears to me to be to just stop the thread that's blocked on the Console
input method.

If it were me, I would investigate whether the ReadTimeout property is
supported for the Console input stream and use that if it is. If it's
not, I'd probably go with the first method (polling), even though I don't
like polling very much. I like aborting threads even less than I like
polling. :)

Pete
Apr 2 '07 #2
On Apr 2, 12:03 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 01 Apr 2007 23:20:29 -0700, SriBhargav <srinivasa.h...@gmail.com>
wrote:
[...]
I would like the user to input something through Console.readline() in
5 secs. If there is no input in that time, I would like to proceed
further with the program logic.
I had a difficulty in implementing this, as console.readline()
indefinitely waits for the user input.
Can somebody help me to implement this in c#?

I've got to say, your question provided me with the opportunity for a
little chuckle. Not because of your post per se, but I decided to browse
the docs a little in the Console class and I found a pretty funny pair of
document pages:

http://msdn2.microsoft.com/en-us/lib...sole.read.aspx

In the former (the docs for Console.ReadLine), they recommend using
Console.Read so as to "produce robust code". In the latter (the docs for
Console.Read), they claim that Console.ReadLine is preferable to using
Console.Read. It's like the two methods were documented by different
writers, and neither really wants "their" method to be used.

I hope no one at Microsoft wonders why it is people sometimes get confused
trying to figure out .NET. :)

Anyway, as far as the actual question goes, it seems to me that there are
at least a few possibilities:

* Use Console.KeyAvailable and Console.ReadKey together to check for
input. Since you'd have to poll the Console in order to avoid making the
user wait too long when they DO provide input, this seems to me the least
desirable solution. However, in a console application where you're
waiting for input and aren't doing anything else anyway, I don't suppose
it would hurt anything to just check for user input every quarter second
or so (obviously you don't want to just poll without any sort of delay
between checks, and I think a quarter second would probably be sufficient
to allow your application to appear to be responsive).

* Get the input stream for Console and then set the ReadTimeout
property of that stream. Note that I don't actually know for sure that
the default input stream for Console actually supports the ReadTimeout
property. You'll have to check that yourself. But if it does, perhaps
that would do what you want.

* Read the input on a separate thread (using Read or ReadLine) and
maintain a timer that aborts the thread if no user input has been provided
by a certain time. Yes, it involves aborting the thread and personally I
don't like doing that. However, as far as I know there's no mechanism to
cause the Console input method to be interrupted, and so the only option
appears to me to be to just stop the thread that's blocked on the Console
input method.

If it were me, I would investigate whether the ReadTimeout property is
supported for the Console input stream and use that if it is. If it's
not, I'd probably go with the first method (polling), even though I don't
like polling very much. I like aborting threads even less than I like
polling. :)

Pete
Thanks for the quick reply... I would try out them

-Srinivas

Apr 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Guy | last post by:
Ok this is might take some exsplaining as this is just example code, I have a telnet server which I've created, its ment to be a a process queue control thing. One of the things I think that would...
1
by: ArunPrakash | last post by:
Hi, I want to implement a method which will poll database for messages. But i want to provide a method that will specify a timeout until which it will be polled and after that it will throw an...
5
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
1
by: Christian Urbanczyk | last post by:
Hello! I have a problem with the Webrequest. I've search everywhere to find an answer but it seems that no one has the the problem before. So i hope somebody can help me here! Following Code:...
1
by: Joachim | last post by:
Is there a way to set a timeout for the Console.ReadLine method?
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
1
by: Kerem Gümrükcü | last post by:
Hi All, i am too tired now to find this out by my own, but why does my while loop "while(readline==Console.ReadLine())" needs twice return hit to read the line, after setting...
8
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.