简体   繁体   English

在类之间传递ManualResetEvent对象

[英]Passing a ManualResetEvent object between classes

My program contains multiple threads, which I want to use a single ManualResetEvent object to control. 我的程序包含多个线程,我想使用一个ManualResetEvent对象进行控制。 When I create a new object (eg. new ManualResetEvent MRE = new ManualResetEvent(args);), I want MRE to be accessible by all classes, yet it is cumbersome constantly passing it as a parameter. 当我创建一个新对象时(例如new ManualResetEvent MRE = new ManualResetEvent(args);),我希望所有类都可以访问MRE,但是不断地将其作为参数传递很麻烦。 Would I be better off making it static? 将其设为静态会更好吗? Or is there a more efficient way of doing so? 还是有更有效的方法? -Thanks -谢谢

Hiding dependencies is a wrong practice. 隐藏依赖项是错误的做法。 If all your classes need the same instance of a ManualResetEvent is not cumbersome to add it as a dependence, it is the expected interface. 如果所有类都需要同一个ManualResetEvent实例,那么将其添加为依赖项并不麻烦,那么它就是预期的接口。

The constructor of an object must show the dependencies the object it has. 对象的构造函数必须显示其所具有的依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM