C# IOThread

在看微软的ASP.NET - 将 ASP.NET 用作高性能文件下载器 示例里面用到了IO 线程,以前打算自己撸的,这里贴出来 已标记一下://////////////////////////////////////////////////// // A delegate's BeginInvoke runs on a "worker thread" inside the CLR Th
摘要由CSDN通过智能技术生成

在看微软的ASP.NET - 将 ASP.NET 用作高性能文件下载器 示例里面用到了IO 线程,以前打算自己撸的,这里贴出来 已标记一下:


    // A delegate's BeginInvoke runs on a "worker thread" inside the CLR ThreadPool.
    // This allows you to run a delegate on a "completion port thread" inside the CLR ThreadPool
    // instead of a "worker thread" by calling IOBeginInvoke instead of BeginInvoke.
    // In a nut shell, this mechanism basically skips the actual system I/O and simply posts a
    // completion delegate to the completion port queue so the completion delegate will be 
    // executed by a "completion port thread" that is working the completion port queue.
    //
    // Example:
    // delegate.BeginInvoke => executes delegate on "worker thread".
    // delegate.IOBeginInvoke => executes delegate on "completion port thread".
    // 
    //
    // Extremely simplified explanation:
    //
    // CLR ThreadPool is made up of two pools of threads: "worker threads" and "completion port threads".
    //
    // Basically you can either queue a user work item which runs the delegate on a "worker thread",
    // or queue a native overlapped item which runs the completion delegate on a "completion port thread".
    // ThreadPool.QueueUserWorkItem (and delegate.BeginInvoke) => executes delegate on "worker thread".
    // ThreadPool.UnsafeQueueNativeOverlapped => executes completion delegate on "completion port thread".
    //
    //             (CLR ThreadPool)
    //               /        \
    // [worker threads]      [completion port threads]
    //
    //  o o  oo  o  oo                    _____________post to queue using ThreadPool.UnsafeQueueNativeOverlapped
    //    o o oo o  o                    |             (i.e. PostQueuedCompletionStatus)
    //   o  o o  o o                     v
    //  oo o  oo  o o                  |  |
    //    o  oo oo o                   |  | <----------completion port queue (one per process)
    //                                 |__|
    //       ^                         oo o
    //       |                        o o oo <---------completion port threads (work the completion port queue)
    //       |                 (cpu)(cpu)(cpu)(cpu)                            (i.e. GetQueuedCompletionStatus in loop)
    //       |                               
    //       |                               ^
    //       |                               |
    //       |                               |
    //       |                               |
    //       |                               |
    //       |    Each individual completion delegate is given to the completion port queue to execute,
    //       |    and the "completion port threads" working the completion port queue execute the delegate.
    //       |    (This has much less risk of thread explosion because each delegate just gets posted to the
    //       |     completion port queue, instead of being given to its own individual thread. Basically
    //       |     the queue grows, n
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#网页版+客户端版聊天软件源码分享(C#+长连接+Sqllite数据库实现) 今天我给大家分享一个聊天程序的源码。 网页版加客户端版并存,可以互通 我相信对大家学习和扩展这一块知识是很有用的。 我们先来看下软件结构 一个Web版一个网页版,而客户端是连接的网页的 http://localhost:53947/wwwroot/Lesktop 这个路径 http://localhost:53947/wwwroot/这一部分是网页的地址,大家可以根据自己配置情况进行修改 然后浏览一下Default.aspx页面如下 这是负面版的,客户端的也是一样的,我们先来注册 一个账户 在这里我们注册两个账户还有一个是text用来聊天对话使用 注册的方法是一样的我就不多说了。 下面登录第一个账户看看 这是登录后的效果。 单击聊天室 然后我们再登录另外一个账户text 好了大家应该能看到效果了吧。 然后咱们再发个“你好” 收到了吧,再回复一个 对就是这个效果, 再来看看桌面版的 刚才的消息都在 这是桌面版的效果。 大家感觉怎么样。 我感觉大家可以在这个基础之上进行扩展,最少可以看看他的实现思路 源码分享给大家了 sufeinet.com即时通信_云骞.zip (3.25 MB, 下载次数: 1078) ReceiveResponsesHandler 类,这个主要是用来接收和维护长连接的 实现长连接的两个重要来代码预览 [C#] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Web; using System.Xml; using System.Threading; namespace Core { public class ReceiveResponsesHandler : IHttpAsyncHandler { public ReceiveResponsesHandler() { } HttpContext m_Context = null; IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) { m_Context = context; string sessionId = context.Request.Params["SessionID"]; string clientVersion = context.Request.Params["ClientVersion"]; string serverVersion = context.Request.Params["ServerVersion"]; ResponsesListener asyncResult = new ResponsesListener(sessionId, cb, extraData); try { if (serverVersion != ServerImpl.Instance.Version) throw new IncompatibleException();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值