.net - Adding/Removing COM Ports from a ComboBox in C# -
.net - Adding/Removing COM Ports from a ComboBox in C# -
i attempting write programme utilizes combobox display connected com ports obtained next method:
system.io.ports.serialport.getportnames()
the thought initialize thread checks available com ports every second, , update combobox accordingly. despite best efforts, can't work.
the code update combobox's contents 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 console messages ports found, combobox remains empty. help appreciated.
try modify code this.
bindinglist<string> availports = new bindinglist<string>(); autoresetevent autoresetevent = new autoresetevent(false); private void form1_load(object sender, eventargs e) { thread t = new thread(new threadstart(update)); t.start(); autoresetevent.waitone(); this.combobox1.datasource = availports; } 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); } } autoresetevent.set(); } }
c# .net com
Comments
Post a Comment