我正在编写一个程序来监视网络上各种设备的状态。这些设备的详细信息存储在一个文件中。HandleClientComm 类从文件中读取有关这些设备的信息并与它们建立 tcp 连接。读取文件后,使用 manEvent 进行通知。为每个设备调用 UpdateGUI 函数。当 data.caller 等于 1 时,它会为该设备添加控件,但禁用组框。函数 hd.StartThreads 使用 Threadpool 侦听来自各种设备的连接。接受连接后,将再次调用 UpdateGUI 函数,其 data.caller 值为 2。我的问题是未启用 groupbox。消息框显示“开始”,但没有结束。尝试访问除组框以外的其他控件,但发现我无法从那里访问任何控件。消息循环是否有问题,因为我的代码中没有运行无限循环?
namespace FA
{
public partial class EditDevice : Form
{
public struct DisplayComponents
{
public GroupBox gp;
public List<Panel> labelPanel;
public List<FlowLayoutPanel> picPanel;
public List<Label> LabelList;
public List<PictureBox> Pics;
public Label Mid, Date, Time;
public int gpHeight, gppt;
};
public DisplayComponents[] comps;
private DeviceList[] dev;
private ManualResetEvent manEvent;
private int devCount;
private HandleClientComm hd;
public EditDevice()
{
InitializeComponent();
//Create event to notify whether device file has been read
manEvent = new ManualResetEvent(false);
//Create object of the client communication class
hd = new HandleClientComm(manEvent);
//wait for the file read notification
manEvent.WaitOne();
//get the device count
devCount = hd.devCount;
//get the device details
dev = hd.dv;
initializeForm();
//Add event handler for device status change
hd.StatusChanged += new HandleClientComm.StatusChangeHandler(UpdateGUI);
//Start thread to monitor device status
Thread th = new Thread(hd.StartThreads);
th.Start();
th.Join();
}
public void initializeForm()
{
//Create components
comps = new DisplayComponents[hd.devCount];
// Groupbox initial point
int gppt = 40;
//Calculate Groupbox point and heights for each devices
for (int i = 0; i < devCount; i++)
{
comps[i].gpHeight = 60;
comps[i].gpHeight = comps[i].gpHeight + (dev[i].Zones / 21) * 77;
if (dev[i].Zones % 21 != 0)
comps[i].gpHeight += 77;
comps[i].gppt = gppt;
gppt += comps[i].gpHeight+10;
}
}
private void UpdateGUI(object sender, StatusChangeEventArgs data)
{
if (data.caller == 1)
{
//Function to add controls to the form
addDeviceControls(data.index);
}
else
{
MessageBox.Show("begin");
comps[data.index].gp.Enabled = true;
MessageBox.Show("end");
}
}
}
public class StatusChangeEventArgs : EventArgs
{
public int index { get; internal set; }
public int caller { get; internal set; }
public StatusChangeEventArgs(int index1, int callno)
{
this.index = index1;
this.caller = callno;
}
}
}