事件定义中为何出现 Lock关键字?
namespace WrapTwoInterfaceEvents
{
using System;
public interface IDrawingObject
{
// Raise this event before drawing
// the object.
event EventHandler OnDraw;
}
public interface IShape
{
// Raise this event after drawing
// the shape.
event EventHandler OnDraw;
}
// Base class event publisher inherits two
// interfaces, each with an OnDraw event
public class Shape : IDrawingObject, IShape
{
// Create an event for each interface event
event EventHandler PreDrawEvent;
event EventHandler PostDrawEvent;
object objectLock = new Object();
// Explicit interface implementation required.
// Associate IDrawingObject's event with
// PreDrawEvent
event EventHandler IDrawingObject.OnDraw
{
add
{
lock (objectLock)
{
PreDrawEvent += value;
}
}
remove
{
lock (objectLock)
{
PreDrawEvent -= value;
}
}
}
// Explicit interface implementation required.
// Associate IShape's event with
// PostDrawEvent
event EventHandler IShape.OnDraw
{
add
{
lock (objectLock)
{
PostDrawEvent += value;
}
}
remove
{
lock (objectLock)
{
PostDrawEvent -= value;
}
}
}
// For the sake of simplicity this one method
// implements both interfaces.
public void Draw()
{
// Raise IDrawingObject's event before the object is drawn.
EventHandler handler = PreDrawEvent;
if (handler != null)
{
handler(this, new EventArgs());
}
Console.WriteLine("Drawing a shape.");
// RaiseIShape's event after the object is drawn.
handler = PostDrawEvent;
if (handler != null)
{
handler(this, new EventArgs());
}
}
}
public class Subscriber1
{
// References the shape object as an IDrawingObject
public Subscriber1(Shape shape)
{
IDrawingObject d = (IDrawingObject)shape;
d.OnDraw += new EventHandler(d_OnDraw);
}
void d_OnDraw(object sender, EventArgs e)
{
Console.WriteLine("Sub1 receives the IDrawingObject event.");
}
}
// References the shape object as an IShape
public class Subscriber2
{
public Subscriber2(Shape shape)
{
IShape d = (IShape)shape;
d.OnDraw += new EventHandler(d_OnDraw);
}
void d_OnDraw(object sender, EventArgs e)
{
Console.WriteLine("Sub2 receives the IShape event.");
}
}
public class Program
{
static void Main(string[] args)
{
Shape shape = new Shape();
Subscriber1 sub = new Subscriber1(shape);
Subscriber2 sub2 = new Subscriber2(shape);
shape.Draw();
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
/* Output:
Sub1 receives the IDrawingObject event.
Drawing a shape.
Sub2 receives the IShape event.
*/
上面一段是MSDN的源码,关于interface + event的,
让我不明白的是:下面,这里为何要用lock关键字, 还有定义了一个object。
object objectLock = new Object();
// Explicit interface implementation required.
// Associate IDrawingObject's event with
// PreDrawEvent
event EventHandler IDrawingObject.OnDraw
{
add
{
lock (objectLock)
{
PreDrawEvent += value;
}
}
remove
{
lock (objectLock)
{
PreDrawEvent -= value;
}
}
}
我自己模仿的时候如下:没用lock关键字和 那个object,也可以正常运行
interface myin
{
event mylele myevent;
}
class zi : fu,myin
{
//event mylele shijian
//{
// add { this.myevfu += value; }
// remove { this.myevfu -= value; }
//}
event mylele _mye;
event mylele myin.myevent
{
add
{
//ock (objectLock)
//{
_mye += value;
//}
}
remove
{
_mye -= value;
}
}
public void shuohua()
{
Console.WriteLine("shuohua:");
if(_mye!=null)
{
_mye(this);
}
}
}
我网上查资料的时候,发现lock关键字的相关信息都是在多线程的时候用的,这里涉及到多线程吗?为何这里要加这个,像我上面的代码那样不加,以后会不会造成什么潜在的漏洞,谢谢大神们~!!
相关推荐
C#event线程安全
突然想到有关C#中使用event特性时关于线程安全的问题,以前虽然有遵从“复制引用+null判断”的模式(盲目地),但没有深入了解和思考。为之查询了资料和实验,对此有了进一步的理解。 定义(field-likeevent): 类内raise: 不禁要问,为何要复制引用?多线程下表现如何? 为了解决上面哪些疑惑,我查了一些资料,其中有来自当时C#编译器开发组成员的一
C# 中的 Lock 关键字实践指南
Monitor在C#中,Mutex和Semaphore是两种用于线程同步的原语。它们通常用于跨进程共享资源的同步,但也可以在单个应用程序内部使用。MutexMutex(互斥体)是一种同步原语,用于在多个线程或进程之间进行互斥。它确保在任何给定时间只有一个线程可以访问资源或代码段。Mutex可以有名称,这意味着可以创建命名的Mutex,多个进程可以通过该名称共享同一个Mutex。// 创建或打开一个命名的Mutex,false表示如果不存在则创建try。
2021-2022计算机二级等级考试试题及答案No.19193.docx
2021-2022计算机二级等级考试试题及答案No.19193.docx
c#多线程中Lock()关键字的用法小结
本文介绍C# lock关键字,C#提供了一个关键字lock,它可以把一段代码定义为互斥段(critical section),互斥段在一个时刻内只允许一个线程进入执行,而其他线程必须等待。 每个线程都有自己的资源,但是代码区是共享的,即每个线程都可以执行相同的函数。这可能带来的问题就是几个线程同时执行一个函数,导致数据的混乱,产生不可预料的结果,因此我们必须避免这种情况的发生。 其中,lock是一种比较好用的简单的线程同步方式,它是通过为给定对象获取互斥锁来实现同步的。它可以保证当一个线程在关键代码段的时候,另一个线程不会进来,它只能等待,等到那个线程对象被释放,也就是说线程出了临界区。用法:
Java synchronized关键字和Lock接口实现原理
主要介绍了Java synchronized关键字和Lock接口实现原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下


