473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need a better way to post data from background thread to main thread

Hello.

I've developed an application that reads at a constant fast rate, bytes from a serial port, and posts these to a textbox on the main form. Basically a serial port monitor of sorts (although the application is for 2-way communication with a specific tool). The tool constantly streams up bytes at a fast rate.

the issue I've run into is I need a better way to put each byte onto the textbox in the main form, without churning up a lot of CPU.

Here's a shortened version of what I'm working with, and I have narrowed down the constant .invoke call as the reason for CPU processing power being used up. Correct me if this is wrong and something else is causing the massive usage of the CPU (right at about 50%).




namespace serialToolComm
{
public partial class Form1 : Form
{
public delegate void delegateSetText(string s);

public Form1()
{
InitializeComponent();
bworker.RunWorkerAsync();
}

private void bworker1_DoWork(object sender, DoWorkEventArgs e)
{
while (serialPort1.IsOpen)
{
if (backgroundWorker1.CancellationPending)
{
Thread.Sleep(100);
e.Cancel = true;
break;
}
else if (serialPort1.BytesToRead > 0)
{
int b = serialPort1.ReadByte();
setText(b.toString() + ", ");
}
else
{ Thread.Sleep(40); }
}
}

private void setText(string s)
{
if (this.textBox1.InvokeRequired)
{
delegateSetText d = new delegateSetText(setText);
this.Invoke(d, new Object[] { s });
}
else
textBox1.AppendText(s);
}
}
}



as you can see and guess this method for use with the delegate is being called rapidly and requiring a lot of CPU usage.
Jun 23 '08 #1
1 970
Plater
7,872 Expert 4TB
Don't post each byte at a time, wait till you have a few available and then post them? Just waiting for 2 bytes instead of every one will cut your usage in like half
Jun 23 '08 #2

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

Similar topics

3
by: P Wolpert | last post by:
This is my first post. I hope I don't sound stupid. I have a script conflict when I put two scripts on one page. Both scripts will work if I use one at a time but the menu button script will not...
10
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
1
by: Srinivasa Ra via .NET 247 | last post by:
(Type your message here) I am writing an application that does lot of read/write's withcomputer's serial port. The application as a whole is workingfine. Current Approach: I have a Timer that...
10
by: Paul | last post by:
Hi, I'm building a database project and have just started getting away from my newbie habit of storing, say, User names in a User field, as text. I've now replaced the Name field with a NameID...
4
by: dsimmons | last post by:
On my website http://seasidequilters.blogspot.com/ I'm having a problem that someone thinks might be associated with Firefox browsers. I'm hoping to find someone in this forum that might be...
7
by: John J. Hughes II | last post by:
I have a DataGridView with a TextBoxColumn. I setting the data source to a List<stringvalue in a static class. The list is filled from a background thread. So far all is fine and it works...
0
by: zeckdude | last post by:
Hi, I am having some issues with my site. I have a main Nav with four links that load in 4 different sections. You can see the page I am working on here:...
3
by: babai28 | last post by:
Hi there, I have a situtation, I am on my way to build an alarm application which will run in the background and will intimate the user for an event which he had set before. Exactly what a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.