none
How to restart timeout timer in ManualResetEvent.WaitOne(Timeout)? RRS feed

  • Question

  • Hi,

    Happy New Year! Thanks for your attention to the question. I'm writing a program to do telnet. When I send a command to remote server, I need to wait until the command execution is done before I can send the next command. I'm using a ManualResetEvent.WaitOne(20000) in the send procedure. And the ManualResetEvent will be set in the receive procedure if an expected prompt appears. The problem is some command execution may take extremely long time such as an hour, but the server keeps sending some data during the execution. So I'd like to restart the timer (20000) whenever new data come. I'm not sure if this is possible with ManualResetEvent. Please feel free to comment or suggest a way to do it.

    Thanks a lot!

    Shaoxing

    Wednesday, January 6, 2016 3:24 PM

Answers

  • When you want to restart the timer, maybe set the event, and also set some additional variable to distinguish between normal event and restart. The thread that waited for event will analyse the variable and will either continue normal processing or restart the waiting.

    Instead of this additional variable you can use another event object, which will be set when other data arrive. The program will use WaitHandle.WaitAny with two events instead of WaitOne. Then will either continue or restart the waiting according to signaled event.


    • Edited by Viorel_MVP Wednesday, January 6, 2016 6:06 PM
    • Marked as answer by Jeff4554716 Thursday, January 7, 2016 12:18 AM
    Wednesday, January 6, 2016 6:05 PM

All replies

  • When you want to restart the timer, maybe set the event, and also set some additional variable to distinguish between normal event and restart. The thread that waited for event will analyse the variable and will either continue normal processing or restart the waiting.

    Instead of this additional variable you can use another event object, which will be set when other data arrive. The program will use WaitHandle.WaitAny with two events instead of WaitOne. Then will either continue or restart the waiting according to signaled event.


    • Edited by Viorel_MVP Wednesday, January 6, 2016 6:06 PM
    • Marked as answer by Jeff4554716 Thursday, January 7, 2016 12:18 AM
    Wednesday, January 6, 2016 6:05 PM
  • Thanks so much Viorel_; this solved my problem perfectly!
    Thursday, January 7, 2016 12:21 AM