Making variables captured by a closure volatile
问题 How do variables captured by a closure interact with different threads? In the following example code I would want to declare totalEvents as volatile, but C# does not allow this. (Yes I know this is bad code, it's just an example) private void WaitFor10Events() { volatile int totalEvents = 0; // error CS0106: _someEventGenerator.SomeEvent += (s, e) => totalEvents++; while(totalEvents < 10) Thread.Sleep(100); } EDIT : People seem to be missing the point of my question a bit. I know I can't use