【发布时间】:2012-03-19 23:02:02
【问题描述】:
在我的智能设备应用程序中,我具有搜索所有可发现的蓝牙设备并连接到带有 Windows Mobile 6.5 的设备的功能。当我按下按钮搜索蓝牙设备时,UI 冻结,我无法执行任何其他操作。找到所有可发现的设备后,UI 再次响应。
我知道我应该使用线程来处理这个问题。但是,我没有成功让它工作。
这是我用于搜索蓝牙设备的代码。在代码中,我有两个 BindingList。一个是 DiscoverableDevices,另一个是 ConnectedSEMDevices,它们分别绑定到一个列表框和一个组合框。
private void SearchBTDevices()
{
// Thread thread = new Thread(new ThreadStart(delegate{
List<BluetoothDevice> list = new List<BluetoothDevice>();
this.discoverableDevices.Clear(); //DiscoverableDevices is binding to the form
list.foreach(x => this.Discoverable.Add(x));
ConnectedSEMDevices.Clear()
list.Where(x => x.HasAuthenticated).ToList().ForEach(x => ConnectedSEMDevices.Add(x)); // ConnectedSEMDevices is binding to the Form
// }));
// thread.Start();
}
当我在上面的代码中取消注释线程时,它什么也不做,也没有找到设备。在我注释掉线程后,它可以工作。有谁知道原因?我想以与正在搜索的设备相同的形式执行其他操作。
【问题讨论】:
标签: c# winforms .net-3.5 thread-safety