EventHandler: Is event always in the same thread? And what about thread safety?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Looking at http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(%22SYSTEM.EVENTHANDLER%601%22);k(SOLUTIONITEMSPROJECT);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&rd=true" target="_blank
this msdn documentation, I have 2 questions:
1)<br/>
My test shows that the event handler "SampleEventHandler" always occurs in the same thread as the "Main" method is running in. Is this always so? I ask because if I work in a UI application (WindowsForms) the event is always occurring in a different thread
than the UI.
2)<br/>
It says in a comment in method "DemoEvent": "Copy to a temporary variable to be thread-safe". Can someone explain why that is nescessary or a good idea? How does a copy to a local copy mean thread safety?
-- <br/>
Werner<br/>



View the full article
 
Back
Top