By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,695 Members | 1,284 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,695 IT Pros & Developers. It's quick & easy.

Best way to implement a background infinite loop

P: 1
In my Forms application, I want to run a background loop that executes ReadFile (P/Invoke) continuously. ReadFile will block waiting for data (to arrive from a USB device) and when the read completes, a Byte[] buffer needs to be sent to the main Form. I'm using BackgroundWorker, and my read loop is in BackgroundWorker_DoWork. I start the background process by calling RunWorkerAsync().

My question is this: is it better coding practice to have a while (true) {...} loop in BackgroundWorker_DoWork that never exits (until the Form is closed) using the ProgressChanged event to send the buffer back to the calling Form, OR is it better to exit _DoWork when the bytes have been read (and not use ProgressChanged), and pass those bytes back to the main Form via the e.Result object. Then in RunWorkerCompleted, call RunWorkerAsync() to restart the background loop?

Any thoughts on pros/cons of each approach or the best way to do this would be appreciated!
Jan 9 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.