My program contains multiple threads, which I want to use a single ManualResetEvent object to control. 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. Would I be better off making it static? Or is there a more efficient way of doing so? -Thanks
Asked
Active
Viewed 1,128 times
0
-
1Push it to another singleton class having a manualresetevent. Yep ManualResetEvent will still be instance object inside singleton. Such a way you can use it any where. But keep in mind that static objects life time is still the end of AppDomains. – Zenwalker Jul 17 '12 at 03:04
-
@zenwalker Considering your final statement regarding the lifetime of static variables, is there any alternative I might want to seek considering that my program is intended to run continuously in the background? – Ari Jul 17 '12 at 03:08
-
@jay, it sounds like we could answer this question better with an example of what you are using the MRE for. You can certainly make a "global static" MRE, but chances are that's too coarse-grained and probably violates some design best practices for the classes that use it. – Chris Shain Jul 17 '12 at 03:17
-
@jaykreeler My point was just a caucious hint. Not intended to scare you off. There is no harm in using static. But we have to use it wisely or else we may have to pay later. Now in your case, i guess its pretty much ok to push it under singleton. Other way you can do is load every ting related to ManualResetEvent into a seperate AppDomain, run it as long as you want and then unload that domain. So you can load and unload whenever you want. But then you have to write cross domain code to share data. Hence to avoid such spaghetti work/code being done, just use singleton. – Zenwalker Jul 17 '12 at 03:28
1 Answers
0
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.
The constructor of an object must show the dependencies the object it has.

Ignacio Soler Garcia
- 21,122
- 31
- 128
- 207