質問

I am writing a program to monitor the status of various devices across a network. The details of these devices are stored in a file. The HandleClientComm class reads information about these devices from the file and establishes a tcp connection with them. After the file has been read, the manEvent is used for notification. The UpdateGUI function is invoked for each device. When the data.caller is equal to 1, it adds controls for that device, but the group boxes are disabled. The function hd.StartThreads, listens for connections from various devices using the Threadpool. Once the connection is accepted, the UpdateGUI function is invoked again with a data.caller value of 2. My problem is that the groupbox is not being enabled. The message box displays "begin", but does not got to end. Tried accessing other controls, other than the groupbox, but found out that I cannot access any of the controls from there. Is it a problem with the message loop, because there is no infinite loop running in my code?

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;
      }
  }
}
役に立ちましたか?

解決

It sounds like your HandleClient.Comm.StatusChangehandler(UpdateGUI); call is consuming the errors or something higher level is catching them. Have you tried to break pointing and stepping through the code?

In order to update controls from different thread, you need to Invoke the changes on your main thread.

Please see this question as it may help you out more. How to update the GUI from another thread in C#?

If you are using WPF, then it is done slightly differently and need to call Dispatcher.Invoke in order to update.

I hope this helps you out. It really does sound like your errors are being consumed without your knowledge.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top