EventWaitHandle

继承:Object->WaitHandle-> EventWaitHandle
派生:System.Threading.AutoResetEvent\System.Threading.ManualResetEvent

AutoResetEvent、 ManualResetEvent只能在进程内使用。semaphore、mutex可以跨进程和线程使用。

使用内核对象记得及时释放句柄和对象。要不会造成内存浪费。

方法:

public static System.Threading.EventWaitHandle OpenExisting (string name);

1、EventWaitHandle.OpenExisting(String eventName) 静态方法,跨进程使用。判段已知的名称的自动事件是否存在

public static bool TryOpenExisting (string name, out System.Threading.EventWaitHandle? result);

1、EventWaitHandle.OpenExisting(String eventName) 静态方法,跨进程使用。判段已知的名称的自动事件是否存在,存在返回true。

AutoResetEvent

只能在进程内使用。

创建自动事件的2种方式:
  1、EventWaitHandle clearCount = new EventWaitHandle(false, EventResetMode.AutoReset);
  2、AutoResetEvent are =new AutoResetEvent(true);//默认打开自动砸门,它自能唤醒一个阻塞线程,在解除第一个阻塞后,内核将事件重置为false,所以第一次waitone()不会阻塞,第二次会阻塞。

方法:

1、EventWaitHandle.OpenExisting(String eventName) 静态方法,跨进程使用。判段已知的名称的自动事件是否存在

 用AutoResetEvent实现一个类似于metux的锁。

AutoMutex am=new AutoMutex();
for (int i = 0; i < 10; i++)
{
new Thread(() => {
am.Enter(); Thread.Sleep(1000);
am.Release(); }).Start();
} class AutoMutex
{ static AutoResetEvent are =new AutoResetEvent(true);
static int lockOwner=-1;//当前锁的拥有者
static int recursionCount=-1;//迭代计数器 public void Enter()
{
int currentcount = Environment.CurrentManagedThreadId;
if (lockOwner == currentcount)
{ recursionCount++;
return;
} are.WaitOne();
Console.WriteLine("Current Thread{0}", Environment.CurrentManagedThreadId);
recursionCount = 1;
lockOwner = currentcount;
}
public void Release()
{
if (lockOwner == Environment.CurrentManagedThreadId) return;
if(--recursionCount == 0)
{
are.Set();
lockOwner = -1;
} } public void Dispose()
{ are.Close();
are.Dispose();
} }

ManualResetEvent

只能在进程内使用。

创建手动事件的2种方式:
  1\EventWaitHandle clearCount =new EventWaitHandle(false, EventResetMode.ManualReset);
  2\ManualResetEvent  are =new  ManualResetEvent(true);//默认打开自动砸门,所以 waitone()不会阻塞,必须手动关闭。

方法:

1、EventWaitHandle.OpenExisting(String eventName) 静态方法,跨进程使用。判段已知的名称的自动事件是否存在

C# Event 内核构造 |EventWaitHandle、AutoResetEvent、 ManualResetEvent的更多相关文章

  1. 【深度好文】多线程之WaitHandle-->派生EventWaitHandle事件构造-》AutoResetEvent、ManualResetEvent

    AutoResetEvent/ManualResetEvent 都是继承自 EventWaitHandle ,EventWaitHandle继承自WaitHandle. 在讨论这个问题之前,我们先了解 ...

  2. C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

    看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...

  3. AutoResetEvent ManualResetEvent WaitOne使用注意事项

    公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...

  4. [.net 多线程]AutoResetEvent, ManualResetEvent

    ManualResetEvent: 通知一个或多个正在等待的线程已发生事件,允许线程通过发信号互相通信,来控制线程是否可心访问资源. Set() : 用于向 ManualResetEvent 发送信号 ...

  5. linux内核构造skb发包-----raw、tcp网络编程

    1. 内核raw发包 #include <linux/init.h>#include <linux/module.h> #include <linux/kernel.h& ...

  6. C#线程学习笔记五:线程同步--事件构造

    本笔记摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/23/Event_Constructor.html,记录一下学习过程以备后续查用. 前面讲的线 ...

  7. .NET中的线程与异步(笔记)

    翻出了之前记录的笔记,基本涵盖了.NET中线程和异步的相关概念.可以提供一个学习的方向. 线程类型 工作者线程IO线程 线程池 全局队列(QueueUserWorkItem.Timer总是放入全局)本 ...

  8. Daemon,Jos,定时器

    --> FileSystemWatcher--> EventWaitHandle / AutoResetEvent / ManualResetEvent--> Mutex--> ...

  9. 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent

    本章主要介绍下基于内核模式构造的线程同步方式,事件,信号量. 阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同 ...

随机推荐

  1. MySQL的innoDB存储引擎的运作方式,数据结构等

    先上InnoDB架构图: 自上而下依次为内存区结构,后台线程,操作系统,磁盘存储,日志文件等. 其中内存由缓冲池,额外缓冲池,日志缓冲池组成.其中缓冲池中结构如下: 在磁盘存储文件中,MyISAM存储 ...

  2. vue学习11-监听属性

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta http ...

  3. GeoServer课程规划

    "凡事豫则立,不豫则废." --西汉·戴圣<礼记·中庸> 为了做好GeoServer课程培训,需要拟定一个课程目录,对整个课程做一个宏观上的规划.有了这个规划,就有了目 ...

  4. java多态成员变量、成员函数(非静态)、静态函数特点

    1 package face_09; 2 3 /* 4 * 多态时, 5 * 成员的特点: 6 * 1,成员变量. 7 * 编译时:参考引用型变量所属类中的是否有调用的成员变量,有,编译通过:没有,编 ...

  5. 集合框架-Map集合-TreeMap存储自定义对象

    1 package cn.itcast.p8.treemap.demo; 2 3 4 import java.util.Iterator; 5 import java.util.Map; 6 impo ...

  6. 沁恒CH32F103C8T6的开发和烧录配置说明

    概述 CH32F1系列是沁恒生产的32位Cortex-M3 MCU, 片上集成了时钟安全机制.多级电源管理. 通用DMA控制器等. 此系列具有 2 路 USB2.0接口.多通道 TouchKey. 1 ...

  7. python数据操作--8

    转:https://www.tuicool.com/wx/MB7nieb 数据类型 整数, 浮点数, 字符串, 布林值(True,False) 列表(list), 不可变的列表 Tuple, 集合(没 ...

  8. React之常用技术栈

     · react-redux:https://www.cnblogs.com/jingxuan-li/p/12439181.html  · react-router-dom:https://www.c ...

  9. Xml的一些基本概念(摘抄自w3school.com.cn)

    一个Xml的文档示例: 点击查看代码 <?xml version="1.0" encoding="ISO-8859-1"?> <note> ...

  10. UCB DS100 讲义《数据科学的原理与技巧》校对活动正式启动 | ApacheCN

    贡献指南:https://github.com/apachecn/ds100-textbook-zh/blob/master/CONTRIBUTING.md 整体进度:https://github.c ...