Terminate a thread after an interval if not returned
问题 I have a thread which grabs some data from network or serial port. The thread must terminate (or return false) if no data is received within 5 seconds. In other words, if running the thread is taking more than 5 seconds it must stop. I am writing in C#, but any .NET language is OK. 回答1: There are two approaches: 1. Encapsulated timeout The thread reading the data from network or serial port can measure time elapsed from its time of start and wait for the data for no more than the remaining