Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# event problem...

  • 27-04-2008 06:07PM
    #1
    Closed Accounts Posts: 1,444 ✭✭✭


    Hi guys, I'm trying to raise an event once the "\n" character is seen on the response stream...

    When the page www.asdf.com/test.php is accessed, it spits out the following (each line takes 5 seconds to appear)
    Hello Master 1<br>\n
    Hello Master 2<br>\n
    Hello Master 3<br>\n
    hwrq = (HttpWebRequest)WebRequest.Create("http://www.asdf.com/test.php");
    hwrs = (HttpWebResponse)hwrq.GetResponse();
    
    Stream rs = hwrs.GetResponseStream();
    StreamReader sr = new StreamReader(rs, Encoding.UTF8);
    string line;
    
    while ((line = sr.ReadLine()) != null)
    {
         label1.Text="new-line char found!";
    }
    rs.Close();
    sr.Close();
    

    Any ideas would be much appreciated. Is there such thing as a buffer event? Or an event that's raised when a certain character enters a buffer?


Comments

  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Howdy chaps,

    I think I've figured it out...

    A simple case of sending lines ending with "\r\n" instead of "\n"!

    Hello Master 1<br>\r\n
    Hello Master 2<br>\r\n
    Hello Master 3<br>\r\n

    HTTP protocol requires that lines end with a carriage return followed by a new line...

    There ya go... :(
    private void examine_stream()
    {
       try
       {
          hwrq=(HttpWebRequest)WebRequest.Create("http://www.asdf.com/test.php");
          hwrs = (HttpWebResponse)hwrq.GetResponse();
    
          Stream rs = hwrs.GetResponseStream();
          StreamReader sr = new StreamReader(rs, Encoding.UTF8);
    
          string line;
          while ((line = sr.ReadLine()) != null)
          {
             label1.text=line;
          }            
          rs.Close();
          sr.Close();
       }
       catch(etc...
    }
    

    I'd welcome any comments about this bit of code:
    while ((line = sr.ReadLine()) != null)
    {
       label1.text=line;
    }
    

    Is that while loop gonna be polling continuously? How can I make this more efficient? (btw, the function examine_stream() runs in its own thread)


Advertisement