C# 无法更新C中的UI组件

C# 无法更新C中的UI组件,c#,events,user-interface,components,message-loop,C#,Events,User Interface,Components,Message Loop,我正在编写一个程序来监控网络中各种设备的状态。这些设备的详细信息存储在一个文件中。HandleClientCommand类从文件中读取有关这些设备的信息,并与它们建立tcp连接。读取文件后,manEvent用于通知。为每个设备调用UpdateGUI函数。当data.caller等于1时,它将为该设备添加控件,但组框将被禁用。函数hd.StartThreads使用线程池侦听来自各种设备的连接。一旦连接被接受,就会再次调用UpdateGUI函数,data.caller值为2。我的问题是没有启用gro

我正在编写一个程序来监控网络中各种设备的状态。这些设备的详细信息存储在一个文件中。HandleClientCommand类从文件中读取有关这些设备的信息,并与它们建立tcp连接。读取文件后,manEvent用于通知。为每个设备调用UpdateGUI函数。当data.caller等于1时,它将为该设备添加控件,但组框将被禁用。函数hd.StartThreads使用线程池侦听来自各种设备的连接。一旦连接被接受,就会再次调用UpdateGUI函数,data.caller值为2。我的问题是没有启用groupbox。消息框显示begin(开始),但不显示Get to end(结束)。尝试访问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.StatusChangehandlerUpdateGUI;呼叫正在消耗错误,或者更高级别的东西正在捕获错误。你有没有尝试过打破定点和单步执行代码

为了从不同的线程更新控件,您需要调用主线程上的更改

请看这个问题,因为它可能会帮助你更多

如果您使用的是WPF,那么它的执行方式会略有不同,需要调用Dispatcher.Invoke进行更新


我希望这对你有帮助。听起来您的错误好像是在不知不觉中被消耗掉的。

使用try-catch尝试捕获执行选项。可能有很多跨线程执行!问题在于调用。