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 6124845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:10:46+00:00 2026-05-23T16:10:46+00:00

I am attempting to write a program that utilizes a ComboBox to display currently

  • 0

I am attempting to write a program that utilizes a ComboBox to display currently connected COM ports obtained from the following method:

System.IO.Ports.SerialPort.GetPortNames()

The idea is to initialize a thread that checks the currently available COM ports every second, and update the ComboBox accordingly. Despite my best efforts, I can’t get it to work.

The code to update the ComboBox’s contents is the following:

    private void Form1_Load(object sender, EventArgs e)
    {
        availPorts = new BindingList<String>();

        Thread t = new Thread(new ThreadStart(update));
        t.Start();
    }

    private void update()
    {
        this.comboBox1.DataSource = availPorts;

        while (true)
        {
            Console.WriteLine("CHECK");

            foreach (String port in System.IO.Ports.SerialPort.GetPortNames())
            {
                if (!availPorts.Contains(port))
                {
                    Console.WriteLine("FOUND");
                    availPorts.Add(port);
                }
            }

            Thread.Sleep(1000);
        }
    }

I can see the console messages as the ports are found, however the ComboBox remains empty. Any help would be greatly appreciated.

  • 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-23T16:10:47+00:00Added an answer on May 23, 2026 at 4:10 pm

    The ComboBox is not being updated because the thread created to run the update method is attempting to update a visual control belonging to another thread. In most cases this would throw an error, however here it does not.

    I solved this problem by first creating a method, separate from update, that only handles the addition of COM port names to the data source. Within this method is an if statement checking if an invoke is required:

    private void addPort(String port)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new addPortDelegate(addPort), port);
            }
            else
            {
                availablePorts.Add(port);
                Console.WriteLine("FOUND");
            }
        }
    

    If an invoke is required, the method is called within the correct thread via a delegate:

    private delegate void addPortDelegate(String s);
    

    This causes the ComboBox to be updated as new COM ports are detected by the continuous execution of the update method. A similar method can be written to remove COM ports that have been disconnected from the system. Just don’t forget to end the thread when the form closes or else it will spin infinitely.

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

Sidebar

Related Questions

I'm currently attempting to write my own program that mirrors the pmap command, specifically
http://pastebin.com/uws2Ts96 - my program so far I'm attempting to write a C program for
I'm attempting to write a program that will generate a text file with every
I am attempting to write a small recursive program that tests a list and
I have been attempting to write a program that will determine if a number
Ok, I need some help here. I'm attempting to write a program that rolls
I'm attempting to write a program in Linux using C++ that counts the number
I've been attempting to write a Lisp macro that would perfom the equivalent of
I am attempting to write an application that uses libCurl to post soap requests
I'm attempting to write a Python C extension that reads packed binary data (it

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.