Race conditions with sleeping thread
I've been told that a program could exploit a sleeping thread with a race condition. How does this work practically? If the following example code (in C#) was compiled, how could the attacker exploit it and more importantly, how is it avoided?

    CreateProcess(null, "calc", IntPtr.Zero, IntPtr.Zero, true, 0x00000020, IntPtr.Zero, null, ref si, ref piS);

    System.Threading.Thread.Sleep(5000);

Is this what's considered a time of check, time of use should the executable (in this case calc.exe) change during the sleep? Is it also possible for another program to start this program, wait for the sleep and create threads/run executables during the sleep all the while "attaching" to the process?
Race conditions with sleeping thread
===

I've been told that a program could exploit a sleeping thread with a race condition. How does this work practically? If the following example code (in C#) was compiled, how could the attacker exploit it and more importantly, how is it avoided?

    CreateProcess(null, "calc", IntPtr.Zero, IntPtr.Zero, true, 0x00000020, IntPtr.Zero, null, ref si, ref piS);

    System.Threading.Thread.Sleep(5000);

Is this what's considered a time of check, time of use should the executable (in this case calc.exe) change during the sleep? Is it also possible for another program to start this program, wait for the sleep and create threads/run executables during the sleep all the while "attaching" to the process?