在C#中从组合框添加/删除COM端口 [英] Adding/Removing COM Ports from a ComboBox in C#

查看:69
本文介绍了在C#中从组合框添加/删除COM端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个使用ComboBox来显示通过以下方法获得的当前连接的COM端口的程序:

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()

该想法是初始化一个线程,该线程每秒检查一次当前可用的COM端口,并相应地更新ComboBox.尽管尽了最大的努力,我还是无法使它正常工作.

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.

更新ComboBox内容的代码如下:

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);
        }
    }

找到端口后,我可以看到控制台消息,但是ComboBox仍然为空.任何帮助将不胜感激.

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

推荐答案

ComboBox未被更新,因为为运行update方法而创建的线程正在尝试更新属于另一个线程的可视控件.在大多数情况下,这会引发错误,但是在这里不会.

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.

我通过首先创建一种与更新分开的方法解决了这个问题,该方法仅处理将COM端口名添加到数据源的问题.该方法中包含一条if语句,用于检查是否需要调用:

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);

这将导致ComboBox被更新,因为通过连续执行更新方法可以检测到新的COM端口.可以编写类似的方法来删除已从系统断开连接的COM端口.只是不要忘记在窗体关闭时结束线程,否则它将无限旋转.

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.

这篇关于在C#中从组合框添加/删除COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆