Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:40:58+00:00 2026-05-27T15:40:58+00:00

I would like some advise on threading as I’m new to it. I’ve read

  • 0

I would like some advise on threading as I’m new to it. I’ve read several articles on threading online.

I’m reading com ports data. I would like to use threading such that it will read the data every 5 seconds and update the data on the listbox. Currently, all the data is being read.
I’m unsure where to start.

Where shall i start putting my thread codes? I’m using Windows Form,c# VS2008.

Here’s my codes to read the data from com ports:

    void datareceived(object sender, SerialDataReceivedEventArgs e)
    {            
        myDelegate d = new myDelegate(update);
        listBox1.Invoke(d, new object[] { });

    }


    public void update()
    {           

        while (serialPortN.BytesToRead > 0)
            bBuffer.Add((byte)serialPortN.ReadByte());
        ProcessBuffer(bBuffer);

    }

    private void ProcessBuffer(List<byte> bBuffer)
    {            

        int numberOfBytesToRead = 125;

        if (bBuffer.Count >= numberOfBytesToRead)
        {            


                listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + "  " + " HR: " + (bBuffer[103].ToString()));


            // Remove the bytes read from the list
            bBuffer.RemoveRange(0, numberOfBytesToRead);

        }

    }        

Thanks all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:40:58+00:00Added an answer on May 27, 2026 at 3:40 pm

    Why not use a timer? Put it into the InitializeComponent method of the form that is appropriate,

    using System.Timers;
    
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Text = "Form1";
    
        Timer timer = new Timer();
    
        timer.Interval = 5000;
        timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
    
        //timer.Enabled = true; // you may need this, but probably not if you are calling the start method.
        timer.Start();
    }
    
    void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        // put your code here to read the COM port
    }
    

    The only other problem with this code is that you will get an exception Cross-thread operation not valid.

    You will have to modify your code like this,

    private void ProcessBuffer(List<byte> bBuffer)
    {
        int numberOfBytesToRead = 125;
    
        if (bBuffer.Count >= numberOfBytesToRead)
        {
            this.Invoke(new Action(() =>
            {
                listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + "  " + " HR: " + (bBuffer[103].ToString()));
            });
    
            // Remove the bytes read from the list
            bBuffer.RemoveRange(0, numberOfBytesToRead);
        }
    }
    

    The reason is that the ProcessBuffer method will be running on a background thread. Background threads cannot access UI components on the UI thread. So you have to call this.Invoke which will run that update to the listbox on the UI thread.

    Have a look here if you want to know more about the Invoke method,

    http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

    UPDATE:

    So within the TimerElapsed method you will want to call your code, but it is not clear to me which part of your code it should call? What is the ‘datareceived’ method for, there is nothing calling it in your code snippet.

    So I am guessing it will be this,

    void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        Update();
    }
    
    public void Update()
    {
        while (serialPortN.BytesToRead > 0)
            buffer.Add((byte)serialPortN.ReadByte());
        ProcessBuffer(buffer);
    }
    

    It doesn’t make sense for it to be calling the ProcessBuffer method because where will the buffer come from?

    If I am not on the right track perhaps expand your code sample, and I am happy to help more.

    Please note a few style changes I made to your code (feel free to take them or leave them), a method in C# should start with a capital letter, and calling the variable bBuffer is not standard in C#. Another point if that method is only called from within the class it should be made private.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are designing a new database and I would like some input in where
Hope you can advise I would like to add some simple fade in out
I would like to get some advise. I have an application which consist of
I'm new for both Sonar and Weld/CDI. I would like your help to advise
I'm in the process of creating a website and would like some advise on
I would like some advise on how to optimise the following while loop: double
Newbie with databases, I would like some advise please.. I have agencies who can
So, who could advise some good Apache Camel routes visual designer? Would like to
I would like some advice on the best approach to use in the following
I am designing RESTful Api's and would like some advice on designing an API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.