timeout on Stream.Read

C

carmelo8

Hi there,

I want to timeout Read method of System.Stream class after 30 secs. of
no activity given a certain opened connection. That is, because some
incoming connections to my application just don't give any signal and
my Read method keeps waiting from the socket for some data to be read
eternally.

I have seen that in framework 2.0 System.Stream.Read has a new property
that will probably match with what I need called 'ReadTimeout'. But i
was wondering how to do that in framework 1.1 that do not has this one.

I have tried to do that with System.Timers.Timer class throwing an
event after 30secs. that create a thread to do some action but i don't
actually need a new thread i just want to timeout an operation (Read)
and after a certain time unblock it and be able to fire some exception
or just handle the "non response from the established connection"
scenario.

If anyone can help me with that or know how to solve it I'll be really
glad. Thanks in advanced.

Carmelo
 
J

Jon Skeet [C# MVP]

I want to timeout Read method of System.Stream class after 30 secs. of
no activity given a certain opened connection. That is, because some
incoming connections to my application just don't give any signal and
my Read method keeps waiting from the socket for some data to be read
eternally.

I have seen that in framework 2.0 System.Stream.Read has a new property
that will probably match with what I need called 'ReadTimeout'. But i
was wondering how to do that in framework 1.1 that do not has this one.

I have tried to do that with System.Timers.Timer class throwing an
event after 30secs. that create a thread to do some action but i don't
actually need a new thread i just want to timeout an operation (Read)
and after a certain time unblock it and be able to fire some exception
or just handle the "non response from the established connection"
scenario.

If anyone can help me with that or know how to solve it I'll be really
glad. Thanks in advanced.

If you close the stream from a different thread, I believe the Read
call will throw an exception.
 
L

Lao Tse

I have tried and it ain't seems to work... Is like the Read() locks the
stream so i can't close it through another thread. Here I post the code
I'm currently using:

public class1
{
myTimer.Elapsed += new
System.Timers.ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 3000;
myTimer.Start();

try
{
count = ins.Read();
}
catch(System.ObjectDisposedException)
{
throw new IOException();
}
myTimer.Stop();
...

private static void OnTimedEvent(object source,
System.Timers.ElapsedEventArgs e)
{
myTimer.ins.Close();
myTimer.Dispose();
}
}
public class MyTimer : System.Timers.Timer
{
public Stream ins;
public MyTimer(Stream ins)
{
this.ins = ins;
}
}

thanks,
Carmelo
 
J

Jon Skeet [C# MVP]

Lao Tse said:
I have tried and it ain't seems to work... Is like the Read() locks the
stream so i can't close it through another thread. Here I post the code
I'm currently using:

What kind of timers are you using? If they're Windows Forms timers, it
would be trying to use the same thread (the UI thread) for both event
handlers - that certainly wouldn't work. You'll need to be in a
different thread.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top