timeout on Stream.Read

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • carmelo8@gmail.com

    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.R ead 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.T imer 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

  • Jon Skeet [C# MVP]

    #2
    Re: timeout on Stream.Read

    <carmelo8@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.R ead 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.T imer 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 - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Lao Tse

      #3
      Re: timeout on Stream.Read

      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.E lapsedEventHand ler(OnTimedEven t);
      myTimer.Interva l = 3000;
      myTimer.Start() ;

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

      private static void OnTimedEvent(ob ject source,
      System.Timers.E lapsedEventArgs e)
      {
      myTimer.ins.Clo se();
      myTimer.Dispose ();
      }
      }
      public class MyTimer : System.Timers.T imer
      {
      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.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: timeout on Stream.Read

        Lao Tse <carmelo8@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 - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...