Subj : Re: threading problem in dll??? Help needed badly. To : comp.programming.threads From : Scott McPhillips [MVP] Date : Thu Jul 07 2005 11:06 pm chuayongquan@hotmail.com wrote: >>What happens if the CS is already owned by another thread at point [1] above? >>The TryEnterCriticalSection() will fail, but you proceed as if >>it didn't. Subsequent calls to LeaveCriticalSection() will cause >>all manner of corruption. >>Cheers, >>-- > > > Then do you have a solution for this. i have been searching for the > answer for weeks... Thx > The obvious solution is to pay attention to the return value from TryEnterCriticalSection: if (TryEnterCriticalSection(..)) { ...access shared stuff LeaveCriticalSection(..); } else ...don't access If you have no need to do something in the 'else' then you should not be using TryEnterCriticalSection. Use EnterCriticalSection instead. It stops your thread until it is safe to enter the protected code. -- Scott McPhillips [VC++ MVP] .