By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,288 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,288 IT Pros & Developers. It's quick & easy.

Can't get Timer to work with StreamReader.

P: n/a
I have a program which is constantly reading from a stream and what I'm
wanting to do is, if the stream hasn't sent anything after a certain amount
of time then do something. I've tried doing this like this...

while(!done)
{
idleTimer.Start();
response = streamReader.ReadLine();
idleTimer.Stop();
....

The interval on the Timer is set to 10000 (10 seconds) but I've hung
response = streamReader.ReadLine() for five minutes and it still doesn't run
the Timer's Tick event. The interesting thing though, is if I do this...

idleTimer.Start();
MessageBox.Show("Testing...");
response = streamReader.ReadLine();
idleTimer.Stop();

....and then don't press the button on the message box, after 10 seconds the
Tick event is fired. So why not after 10 seconds of waiting for a response
from the stream? I've even tried sticking the timer stuff in it's own thread
but I get the same problem.

Please help,

Darrell

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies

P: n/a
My guess is that the StreamReader.ReadLine method hangs the current
thread, which prevents it from pumping messages. MessageBox.Show keeps
pumping messages while the message box is displayed, and that lets the
timer messages be handled.

You could perhaps put the StreamReader code in a worker thread, keep the
timer on the ui thread, and when the timer fires check if anything has
been read.

Regards,
Joakim
redneon wrote:
I have a program which is constantly reading from a stream and what I'm
wanting to do is, if the stream hasn't sent anything after a certain amount
of time then do something. I've tried doing this like this...

while(!done)
{
idleTimer.Start();
response = streamReader.ReadLine();
idleTimer.Stop();
...

The interval on the Timer is set to 10000 (10 seconds) but I've hung
response = streamReader.ReadLine() for five minutes and it still doesn't run
the Timer's Tick event. The interesting thing though, is if I do this...

idleTimer.Start();
MessageBox.Show("Testing...");
response = streamReader.ReadLine();
idleTimer.Stop();

...and then don't press the button on the message box, after 10 seconds the
Tick event is fired. So why not after 10 seconds of waiting for a response
from the stream? I've even tried sticking the timer stuff in it's own thread
but I get the same problem.

Please help,

Darrell

Nov 16 '05 #2

P: n/a
I've tried putting the timer stuff in a new thread but I still get the same
problem. The method with the reader in is a thread off the UI in itself
already.

Darrell

"Joakim Karlsson" wrote:
My guess is that the StreamReader.ReadLine method hangs the current
thread, which prevents it from pumping messages. MessageBox.Show keeps
pumping messages while the message box is displayed, and that lets the
timer messages be handled.

You could perhaps put the StreamReader code in a worker thread, keep the
timer on the ui thread, and when the timer fires check if anything has
been read.

Regards,
Joakim
redneon wrote:
I have a program which is constantly reading from a stream and what I'm
wanting to do is, if the stream hasn't sent anything after a certain amount
of time then do something. I've tried doing this like this...

while(!done)
{
idleTimer.Start();
response = streamReader.ReadLine();
idleTimer.Stop();
...

The interval on the Timer is set to 10000 (10 seconds) but I've hung
response = streamReader.ReadLine() for five minutes and it still doesn't run
the Timer's Tick event. The interesting thing though, is if I do this...

idleTimer.Start();
MessageBox.Show("Testing...");
response = streamReader.ReadLine();
idleTimer.Stop();

...and then don't press the button on the message box, after 10 seconds the
Tick event is fired. So why not after 10 seconds of waiting for a response
from the stream? I've even tried sticking the timer stuff in it's own thread
but I get the same problem.

Please help,

Darrell

Nov 16 '05 #3

P: n/a
redneon... I just finished a sample project that launches I/O threads
and calls WaitForThreadExit(waitForThreadTime). Basically, each I/O
thread callsback when done. The worker thread sleeps in a loop checking
to see if the I/O thread has returned. If the waitForThreadTime is
exceeded the worker thread exits the loop.

http://www.geocities.com/jeff_louie/call_console.htm

Regards,
Jeff
I've tried putting the timer stuff in a new thread but I still get the

same problem.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.