473,513 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

timeout on Stream.Read

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

Oct 27 '06 #1
3 11687
<ca******@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 27 '06 #2
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
If you close the stream from a different thread, I believe the Read
call will throw an exception.
Oct 30 '06 #3
Lao Tse <ca******@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 31 '06 #4

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

Similar topics

0
1431
by: Garnet2 | last post by:
I am trying to get the following code to work : HttpWebRequest request1 = (HttpWebRequest) WebRequest.Create(@"http://localhost/TacUssd/8.tac"); request1.Method = "POST"; request1.Credentials =...
3
15081
by: Michael Fields | last post by:
I have a problem with the WebClient and uploading a file to a web server. The stream object returned from the WebClient.OpenWrite menthod throws a timeout exception when using this method on...
1
3169
by: Joey Chömpff | last post by:
Hello, When I'm uploading an file to a JRun WebServer (third party) with a Windows Forms application, l always get an TimeOut while uploading, all other request who doesn't request an post are...
3
6457
by: Helge Jensen | last post by:
I am implementing a protocol which transmits messages. The messages are most naturally transferred using the a Stream so the protocol can communicate over serial-ports, network links, .... If...
1
2767
by: davvel | last post by:
It has been 6 days of re-writing the same code in different ways to try and avoid a getResponse Timeout which haunted me for much too long now. I am trying to do a very simple thing which is...
3
4521
by: Al G | last post by:
Does anyone know what a -1 means as a Network stream read timeout? Console.WriteLine("Timeout= {0}", stream.ReadTimeout) returns a -1 Thanks
5
1601
by: poe | last post by:
Hello, I currently have a routine that copies the contents of a web request into a string. I would like, however, to set the period after which the reader will throw an exception. I don't want...
10
6126
by: Zytan | last post by:
I have a TcpClient. I set the read/write timeouts at 1 minute (in milliseconds). I get a NetworkStream from it and confirm the timeouts still exist. I do a NetworkStream.Write() and then a...
2
4361
by: Piotrekk | last post by:
Hi I am trying to upload a 700 mb file using webrequest/response model. At 3x% I am getting the same error: "Unable to write data to the transport connection: an established connection was...
0
7254
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,...
1
7094
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...
0
7519
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...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
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...
0
4743
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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...

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.