如何检查HttpListenerRequest连接是否仍然打开?

如何检查HttpListenerRequest连接是否仍然打开?

问题描述:

我想检查连接是否仍然打开。如果不是,我会停止这个过程。如何检查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://*.com/help/mcve –

我已经解决了写入/刷新对的问题。 当连接关闭时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; 
}