我寫一個程序來監控整個網絡的各種設備的狀態的UI組件。這些設備的詳細信息存儲在一個文件中。 HandleClientComm類從文件讀取有關這些設備的信息,並與它們建立tcp連接。文件被讀取後,manEvent被用於通知。爲每個設備調用UpdateGUI函數。當data.caller等於1時,它爲該設備添加控件,但組框被禁用。函數hd.StartThreads使用線程池監聽來自各種設備的連接。一旦連接被接受,UpdateGUI功能與2我的問題data.caller值再次調用是組框沒有被啓用。消息框顯示「開始」,但不會結束。嘗試訪問除groupbox之外的其他控件,但發現我無法從那裏訪問任何控件。這是消息循環的問題,因爲我的代碼中沒有運行無限循環?無法更新C#
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;
}
}
}
嘗試使用的try-catch趕上execption。也許有跨線程錯誤 –