replaced the ugly code with ManualResetEventSlim