473,748 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting timeout for Console.readlin e()

Hi,

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

I would like the user to input something through Console.readlin e() 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.readlin e()
indefinitely waits for the user input.

Can somebody help me to implement this in c#?

Thanks,
Srinivas

Apr 2 '07 #1
2 14384
On Sun, 01 Apr 2007 23:20:29 -0700, SriBhargav <sr************ @gmail.com>
wrote:
[...]
I would like the user to input something through Console.readlin e() 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.readlin e()
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.ReadLin e), they recommend using
Console.Read so as to "produce robust code". In the latter (the docs for
Console.Read), they claim that Console.ReadLin e 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.KeyAvai lable 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...@nn owslpianmk.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.readlin e() 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.readlin e()
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.ReadLin e), they recommend using
Console.Read so as to "produce robust code". In the latter (the docs for
Console.Read), they claim that Console.ReadLin e 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.KeyAvai lable 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
2040
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 be really useful is to be able to use the server as a sort of cmd prompt remote control. I use the same sort of code below I can write a cmd to the popen2 cmd but when reading them it reads but then hangs on the readline there isn't a end of...
1
3406
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 exception. I tried the following code. Here say for example GetString in class test is a method for which i want to provide timeout. Class1 contains the console application's main method
5
6342
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 complete project to show what happens. If anybody has an idea of how to fix this, please let me know. Yes I am aware that this is all in .Net 2. Thanks in advance.
1
5198
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 elegant way to inject CrLf into the input stream, which effectively unblocks the ReadLine method. Last night, I had finally got the WriteConsoleInput
1
2683
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: _________________________________________ WebRequest wrq = WebRequest.Create(URL); WebResponse wrs = wrq.GetResponse();
1
4938
by: Joachim | last post by:
Is there a way to set a timeout for the Console.ReadLine method?
12
6543
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
1
2337
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 "Console.TreatControlCAsInput = true". How can i make it work like it would be set to "false", so that i only must hit the return once to get the input,... TIA
8
16095
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 which modifies Application setting section but how to add new item to userSettings ? Andrus.
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.