private void Flush(TimeSpan timeout) { FlushStopWatch.Reset(); FlushStopWatch.Start(); do { if (GetICommunicationLayer.CheckChar() == TCheckCharRes.DataInd) { GetICommunicationLayer.Flush(); } System.Threading.Thread.Sleep(1); }while (FlushStopWatch.Elapsed < timeout); FlushStopWatch.Stop(); }
protected bool CheckCharTimeout(TimeSpan timeout, Stopwatch stopwatch) { Debug.Assert(stopwatch.IsRunning, "StopWatch is not running"); if (!stopwatch.IsRunning) { // if we are inside this "if" statement something has gone wrong we have to start the stopwatch to prevent application hang stopwatch.Reset(); stopwatch.Start(); } TimeSpan minWaitTime = TimeSpan.FromMilliseconds(20); while (true) { switch (GetICommunicationLayer.CheckChar()) { case TCheckCharRes.DataInd: return(true); case TCheckCharRes.NoDataAvailable: //because the thread can be inactive for a while we have to check once more character availability. if (stopwatch.Elapsed > timeout && GetICommunicationLayer.CheckChar() != TCheckCharRes.DataInd) { return(false); } else { break; } if (stopwatch.Elapsed + minWaitTime < timeout) { Thread.Sleep(1); } break; case TCheckCharRes.DisInd: throw (new DisconnectException()); } } }