Data Function In Its Own Thread?

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
So I've been programming in C# since last Friday or so and I've written a basic program that allows me to talk to the piece of hardware that I am controlling. I can make the hardware do more or less as I want while only using negligible processing power.

The problem I'm having is that getting the data back from the hardware and trying to manage it is very processor intensive. Whether this is a result of the way I'm managing it or not is unclear to me at this point. I'm getting 17 doubles from the hardware every 2 milliseconds, then throwing them in a double-buffered array. The code I'm using to do that is:

for (int i = 0; i < 8; i++)
{
if (ind > ArrayLength - 1)
{
ind = 0;
if (j==1) { j=0; } else { j = 1; }//toggle j to switch buffers
}
Time[ind, j] = g.sourceValue(record, "TIME");
Pos[i, ind, j] = g.sourceValue(record, "TP" + AxesString);
Force[i, ind, j] = g.sourceValue(record, "TT" + AxesString);
ind += ind;//increment the counting variable
}

This executes on an interrupt from the hardware (every 2 ms). It's critical that I don't lose any of this data, so having this run as fast as possible is very important. As I have no formal programming/CS training, I'm not really sure if this is the best way to do it. I thought that I could run this in its own thread to ensure that it has enough CPU time to do its thing, but I'm not really sure how to make that happen. Does anyone have any advice on how I can speed this up/ensure that I don't miss data or on whether this could/should run in its own thread? Like I said, this is amateur hour here so I won't be offended by very simple answers. :p
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
This is a pretty typical consumer-producer example. Your hardware is going to produce data and you can have a thread consuming it(processing it). All you need is a struct/class to hold your data and a Queue to buffer the incoming data. Your hardware interrupt will stuff the struct with data and enqueue it into the Queue. Then your Consumer thread can dequeue from the same Queue to process the next set of data.

You'll have to worry about dead locking though, for something this simple you could get by with simply using a lock(some object) { } around both the enqueue and dequeue operations. How you control your data processing thread is up to you. If you think it's going to be starved for data you can use an AutoResetWaitHandle to have the thread block until your enqueue method notifies the thread to continue, otherwise you can let the loop spin while the Queue is empty with a short sleep statement depending on long you want to delay the thread from processing data.
 

engineereeyore

Platinum Member
Jul 23, 2005
2,070
0
0
I'd agree with Crusty. If you're concerned about missing data, you could create a thread and have it do nothing but handle the interrupt and store the data into some queue or memory buffer. Then just have your main thread process the data as it is added by the other thread. That way you don't miss any data, and you process it with whatever processing power you have left over. Just be sure to make the buffer/queue large enough that it won't overflow before the main thread has a chance to run again and process the data.

Just a question though. Are you sure "ind += ind;" is what you want? I'm not sure how you initialize the variable before the code you included, but you set it equal to zero in one of your if statements, and when that happens, it's never going to change. 0 += 0 is still 0. If you're just looking to increment a counter, you might want to use ind++ or ++ind, or ind += 1;
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Thanks guys, and thanks for pointing out the incrementing problem eeyore. I was trying to get this out before I had to leave today so I added instead of incrementing. :eek: I'll try to implement this tomorrow after doing a little reading. :beer::beer: