I've met a very weird (for me..) Exception. It happens only rarely, but does...
My class isn't static, but has only one static attribute:
static Dictionary<string, ManualResetEvent> resetEvents =
new Dictionary<string, ManualResetEvent>();
When I'm trying to add for the first time a reset event - I'm getting, sometimes, a Null Reference Exception. Might this be related to two different Threads trying to add instance?
static ManualResetEvent resetEventsGet(string key)
{
if (resetEvents.ContainsKey(key))
return resetEvents[key];
ManualResetEvent reste = new ManualResetEvent(false);
resetEvents.Add(key, reste); //System.NullReferenceException: 'Object reference not set to an instance of an object.' HOW???
return reste;
}
When I'm looking in the "watch" or immediate window there's no null anywhere (the dictionary or the resetEvent).
p.s - I tagged it for visual studio 2017 because it never happened to me before, although code didn't change. Any idea? Thanks