我正在编写一个程序,以监视网络上各种设备的状态。这些设备的详细信息存储在文件中。HandleClientComm类从文件中读取有关这些设备的信息,并与它们建立了TCP连接。读取文件后,使用该操作进行通知。为每个设备调用UpdateGui函数。当data.caller等于1时,它添加了该设备的控件,但是禁用了组框。函数高清。启动,使用ThreadPool聆听来自各种设备的连接。接收到连接后,再次使用data.caller值再次调用updategui函数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;
}
}
}
听起来您的HandleClient.Comm.StatusChangehandler(UpdateGUI);
调用正在消耗错误或更高级别正在捕获它们。您是否试图打破指向并踏入代码?
为了从不同线程更新控件,您需要调用主线程上的更改。
请参阅此问题,因为它可能会帮助您更多。如何从C#?
中的另一个线程更新GUI如果您使用的是WPF,则进行略有不同的操作,需要致电dispatcher.invoke才能更新。
我希望这对您有所帮助。听起来确实像是在不知情的情况下消耗了您的错误。