Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 Best practices for handling serial data for performance

$
0
0
I have written several programs that read serial data.
One type of program is working with RS-232 Serial data, the other MIDI data. For all intents and purposes, the handling and issues are the same.

The Serial and MIDI libraries run on a separate thread by design. For clarity, I'll call the UI Thread the "UI Thread" and the MIDI or RS-232 Thread the "Serial Thread".

When data is received, an event is raised in the Serial Thread.
The Serial Thread event handler has to be a quick process as any time spent in the event handler can slow down the receiving of data, and in some cases even cause a buffer overflow. This quick handler runs in the Serial Thread, but the data in the buffer is handled by the UI Thread.

One way to handle the data quickly is to have the Serial Thread event handler simply put the incoming data into a buffer, and the UI Thread can have a loop that looks to see if there's any new data, and if there is, pull it out of the buffer and act on it. I've written programs this way and it works.
But, it seems messy to have this loop continuously waiting for data. I'd much rather have an event fired off when there's new data and when it is, the UI Thread can start working on it.
I've done this, using delegates and so on, and it's not really workable if the UI Thread event handler has a lot of processing to do and takes a while. If the Serial Thread event handler that's called when there's new data raises a UI Thread event handler, the Serial Thread is blocked until the UI Thread handler finishes. This doesn't make sense to me, as I thought the UI Thread Event handler would run and the Serial Thread event handler would just continue on in the "background". But in my case it doesn't.
Could just be my limited understanding of multi-threading and maybe there's a way to send an event from the Serial Thread to the UI Thread and not have it stop the Serial Thread?

So I've done it both ways (looping and event raising), but it's just not clear what is best and I don't like either solution.

What is the proper way to code this? Is there a proper way? Is there a better way than the first (looping, waiting for data) method?

Thanks for any and all help/advice!

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>