Why is my code not thread save? - C#