Best way to repeat tasks on a regular time interval.

Using VB.NET 2005 I have a hardware device the constantly sending data to the PC serial port. I was wondering what the best way to poll the serial port for data would be. I was thinking of two different approaches. Any pros/cons or another suggested method would be appreciated. I'm kind of thinking that Option #2 is better programming practice. I'm just not sure where to start.

1) Put a timer on the form. Set the timer to trigger every 10 minutes. When triggered, the code inside the timer reads data from the serial port. This is an interupt of sorts

2) Have a seperate thread that runs in parallel with the main program look. Inside this thread is a loop that looks a the system time. When 10 minutes elapses since the last poll, it will poll the serial port.

Thanks.




Answer this question

Best way to repeat tasks on a regular time interval.

  • CyberShoggy

    It looks from your though that both will work the difference is that one is running on a different thread.

    The Timer is much simpler to develop and you'll set the Poll process in the tick event and set a duration to 10 minutes.

    Sure its on the main UI thread but how long will the process take - if its say a few seconds then I'd definately do approach 1 for simplicity and ease of use.

    I'm pretty sure the Serial Port works on different thread anyway, I recall an issue confirming this with the System.io.SerialPort class recently in one of the VB Forums.


  • Best way to repeat tasks on a regular time interval.