我的主表单中有一个GUI标签。当我按下一个按钮的表单时,会创建一个带有参数的新线程,有些事情会发生,最后我想更新标签说它已完成。但我得到一个NullRefferenceException。我该如何更新?我基本上在不同的项目中使用相同的代码,我只是没有用参数启动Thread。这是我的代码:
GUI_logic.cs:
private void button_upload_Click(object sender, EventArgs e) {
UploadFile upload = new UploadFile();
t_upload = new Thread(() => upload.startUpload(file));
t_upload.Start();
}
public static GUI_logic _GUI_l;
delegate void updateLabelStatusCallback(string text);
public void updateLabelStatus(string message) {
if (this.label_status.InvokeRequired) {
updateLabelStatusCallback d = new updateLabelStatusCallback(updateLabelStatus);
this.Invoke(d, new object[] { message });
} else {
this.label_status.Text = message;
}
}
UploadFile.cs:
public void startUpload(OpenFileDialog file) {
string ext = Path.GetExtension(file.FileName);
switch (ext) {
case ".xml":
parseXMLFile(file.FileName);
break;
}
}
private void parseXMLFile(string file) {
I do stuff here
...
...
//And now I want to update the label
GUI_logic._GUI_l.updateLabelStatus("Done");
}
答案 0 :(得分:0)
好的找到了答案。与建议的评论一样,我没有实例化变量,因此我在代码中添加了_GUI_l=this
public GUI_logic() {
_GUI_l = this;
InitializeComponent();
}