2017-06-02 76 views
0

我想檢查連接是否仍然打開。如果不是,我會停止這個過程。如何檢查HttpListenerRequest連接是否仍然打開?

  //Process request code goes here 

     while (!isComplete) { 
      //Check HttpListenerRequest if connection is still open... 

      if (IsTimedOut && !timeoutTriggered) 
      { 
       timeoutTriggered = triggerTimeout(); 
      } 

      Thread.Sleep(100); 
     } 
     stopWatch.Stop(); 
     //Process response code goes here 

我該怎麼做?

+0

問題根本不明確,請創建https://stackoverflow.com/help/mcve –

回答

0

我已經解決了寫入/刷新對的問題。 當連接關閉時Flush引發異常。

try 
{ 
    //Checks whether the connection is still open 
    var encoding = Encoding.Default; 
    byte[] utf8Bytes = Encoding.Convert(encoding, Encoding.UTF8, encoding.GetBytes(" ")); 
    listenerContext.Response.OutputStream.Write(utf8Bytes, 0, utf8Bytes.Length); 
    listenerContext.Response.OutputStream.Flush(); 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e); 
    eventStream.TriggerEvent(new ActionEventArgs(this, ActionEventStreamer.SYSTEM_CANCEL)); 
    break; 
}