首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查HttpListenerRequest连接是否仍处于打开状态?

如何检查HttpListenerRequest连接是否仍处于打开状态?
EN

Stack Overflow用户
提问于 2017-06-02 20:40:57
回答 1查看 27关注 0票数 0

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

代码语言:javascript
复制
         //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

我怎么能这么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-06 15:22:38

我已经用写/刷新对解决了这个问题。当连接关闭时,刷新引发异常。

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44337122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档