惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - Lance Yang

批量插入Oracle,遇到CLob字段慢的解决办法 c#,利用WPF的ScaleTransform和TranslateTransform实现图片的缩放效果 自定义控件如何给特殊类型的属性添加默认值 z(转) Oracle 12c心得 Entity Framework Code First (八)迁移 Migrations EF Code First学习笔记 C#判断程序是由Windows服务启动还是用户启动 全方位掌握 NSIS 的操作 NSIS学习笔记(转) C#开源框架(整理) 十大前端开发框架(转) .Net 学习 C# 实现无焦点窗体(转载) C++C#时间转换 C#调用C++导出类(转) 自己编码实现数据库的映射实体的代码生成器 C# P/Invoke中传递数组参数 Digital image processing In C# C#数字图像处理(摘录)
Socket异步发送的同步控制
Lance Yang · 2014-03-23 · via 博客园 - Lance Yang

在网络通信中,我们使用Socket异步发送数据,但在客户端,往往是需要等待服务器的返回结果后(握手过程)再往下执行,这就涉及到同步控制了,在多次的实现中,使用AutoResetEvent,实现不,即有时候发现,Socket数据返回比阻塞的UI线程快,不知道是Socket的优先级别高,还是什么原因。总之,这个同步控制是失败的。后来经研究,想到了解决办法,只要发送线程和接收线程同时对ThreadSemaphore进行操作,发送数据时调用Wait方法,收到Socket返回的数据时调用Set方法,问题解决。对于信号量的同步控制有很多场合的应该,不同的场合可以设计不同的实现。看了很多的实现,都是需求相对复杂的,而我的需求就是阻塞UI线程,等待服务器返回结果后再往下执行,所以只能参考别的实现,自己设计(下面程序没有经过测试,只是一个即时设计)。

public class ThreadSemaphore

{

  bool m_isSocketReturn;

  public bool Wait(int timeout)

  {

    lock(this)

    {

      if (!m_isSocketReturn && !Monitor.Wait(this, timeout)

         retun m_isSocketReturn;

    }

    retun m_isSocketReturn;

  }

  pulic void Set()

  {

    lock(this)

    {

      m_isSocketReturn = true;

      Monitor.PulseAll(this);

    }

  }

}