Reputation: 3125
I am streaming data from an inertial sensor into a C# application. Using a bool as a switch, I print the incoming values to a series of text boxes, as so:
if (bStreaming == true)
{
textboxTX.Invoke(new Action(() => textboxTX.Text = Convert.ToString(result[0])));
textboxTY.Invoke(new Action(() => textboxTY.Text = Convert.ToString(result[1])));
textboxTZ.Invoke(new Action(() => textboxTZ.Text = Convert.ToString(result[2])));
}
The problem is, as soon as I start this loop, the rest of my application slows down considerably and starts to lag. Am I doing this wrong? I have tried starting the loop inside a new thread, and see the same result.
Upvotes: 1
Views: 105
Reputation: 3589
It does not matter what thread calls the invoke method. If it's called in a tight loop it will drain your resources anyway. Try await
ing Task.Delay(millis)
for some time before each iteration. You can even use this as a sort of "minimum time" using the following code:
while (true)
{
var updateFrequency = Task.Delay(1000);
if (bStreaming == true)
{
textboxTX.Invoke(new Action(() => textboxTX.Text = Convert.ToString(result[0])));
textboxTY.Invoke(new Action(() => textboxTY.Text = Convert.ToString(result[1])));
textboxTZ.Invoke(new Action(() => textboxTZ.Text = Convert.ToString(result[2])));
}
await updateFrequency;
}
Upvotes: 1
Reputation: 14929
If data rate is high, then you should consider using batch updates. Instead of updating as soon as data received, consider making updates for each second, or when the number of messages exceeds a threshold.
You can use a backgroundworker to process data and update UI at some intervals. You may refer to this SO question.
Upvotes: 3