我正在通過this tutorial
關於如何在後臺執行一些工作以及在這段代碼中我很困惑,爲什麼在調用ReadTheFile(filename)
方法之前沒有顯示消息reading the file...
。爲什麼這個順序操作沒有被執行?
private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
lblResults.Text = " ... reading the file ...";
FileReader1 fr = new FileReader1();
int numLines = fr.ReadTheFile(ofd.FileName);
lblResults.Text = string.Format("We read {0} lines", numLines.ToString());
}
}
作者通過下面的說明解釋它,但它並沒有真正接觸到我。
Worse, even though we set the label’s Text property before we call ReadTheFile, the message loop doesn’t get a chance to process that change, and update the text, before we go out to lunch in ReadTheFile.
這是什麼意思?這可以用簡單的術語來解釋嗎?
與表單運行的線程有關。在讀取文件之前,用戶界面沒有機會重新繪製。 –