• 用到UdpClient的一点经验


    Thread.Abort对UdpClient.Receive阻塞的线程无效

    http://computer-programming-forum.com/4-csharp/184f9d4ee63704fc.htm

    This is not a bug (just very poorly documented :) 

    Thread.Abort throws an exception only when the thread is in managed code. 
    Since calling UdpClient.Receive call a blocking function of the WinSock API 
    it will not receive the abort exception. Only after UdpClient.Receive 
    finishes (hence you are back in managed code) the Thread.Abort method 
    re-throws the exception. Lookup in the help files at Thread.Abort(). 

    How to solve this problem? 
    Use a Socket class (instead of UdpClient) and use the asynchronously methods 
    BeginReceiveFrom and EndReceiveFrom (of course also for XXXSendTo). These 
    methods are a litter harder to use but your thread now remains in managed 
    code while waiting for the receive operation (still unmanaged code) to 
    complete. 

    -- 
    Sander Leer 

    Quote:
    > If a thread is waiting on a call to UdpClient.Receive, there is no way to 
    > return from this call unless the UdpClient is closed or data is received. 
    > Calling Thread.Abort on the blocked thread has no effect. 

    > The attached code illustrates the problem. 

    > Is this a bug? 

    > DAve 

    封装Socket.BeginReceive/EndReceive以支持Timeout

    http://www.cnblogs.com/dytes/archive/2012/08/13/SocketAsyncOpWithTimeout.html

  • 相关阅读:
    洛谷P3157 [CQOI2011]动态逆序对
    CDQ分治
    快速数论变换(NTT)
    洛谷P3338 [ZJOI2014]力
    洛谷 P1919 A*B Problem升级版
    0-1分数规划
    洛谷P4593 [TJOI2018]教科书般的亵渎
    拉格朗日插值
    20180912-3 词频统计
    20190912-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/s5689412/p/4846908.html
Copyright © 2020-2023  润新知