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

C# 如何检查HttpListenerRequest连接是否仍然打开?,c#,console-application,C#,Console Application,我想检查连接是否仍然打开。如果不是,我会停止这个过程 //Process request code goes here while (!isComplete) { //Check HttpListenerRequest if connection is still open... if (IsTimedOut && !timeoutTriggered) {

我想检查连接是否仍然打开。如果不是,我会停止这个过程

         //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

我怎样才能做到这一点?

我已经用写/刷新对解决了这个问题。 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;
}

问题一点也不清楚。请创建