我設法做出一個簡單的程序,從不同的線程和類更新文本框。以直觀的方式學習線程的最簡單方法。 ;)從一個線程調用工作,但不能做得更簡單?
該代碼是從MSDN的文檔。它運行,我看不到內存泄漏。但我有一種感覺,這可以做得更簡單。
有兩大類: Form和BackGround類。在BackGround類中有一個名爲generateStream的函數,它通過委託給一個名爲updateTextBox的函數更新Form類中的TextBox。
當buttonStart被按下時,會執行兩個操作。首先直接調用generateStream。當generateStream準備就緒時,會再次運行generateStream的線程(通過TheFunction)。當使用不同的線程時,TextBox需要一個調用解決方案。
所有這些都已經實施並且有效。我認爲這是相當多的代碼,也許可以做得更乾淨。
更新表單並被回調的主類中的函數。
public void updateTextBox(string strtext)
{
if (this.InvokeRequired) //When calling from another thread; invoke is required.
{
SetTextCallBack cb = new SetTextCallBack(updateTextBox);
this.Invoke(cb, new object[] { strtext });
}
else //Call can be performed safely.
{
textBoxStatus.SelectionStart = textBoxStatus.Text.Length;
textBoxStatus.AppendText(strtext);
textBoxStatus.Update();
}
}
單擊按鈕完成的操作。
private void buttonStart_Click(object sender, EventArgs e)
{
buttonStart.Enabled = false;
/*
* Step 1: Let's call the function blocking.
* The GUI will not respond to mouse geastures. Hoover mouse over inputbox to see no change in pointer.
*/
bg.generateStream(numberOfCalls, "GUI Thread: ");
/*
* Step 2: Let's call the function in a seperate thread.
* The GUI will now respond to the mouse. Hoover mouse over inputbox to see pointer change.
*/
Thread sepThread = new Thread(new ThreadStart(this.TheFunction));
sepThread.Start();
buttonStart.Enabled = true;
}
BackGround類中的generateStream函數。
public void generateStream(int amountOfStreams, string inpString)
{
for (int i = 0; i < amountOfStreams; i++)
{
Thread.Sleep(1000); //Easy does it.
myCallBack(inpString + i.ToString() + System.Environment.NewLine); //This is the text to the Main Form.
}
}
當使用線程我現在使用兩個回調更新文本框。一個從BackGround類到Form,另一個用於實現Invoke。是否有例如沒有信號量解決方案可能我的updateTextBox函數只能有一個線程訪問?
我不太明白你的問題是什麼,你可以嘗試用你的最後一段來解釋你的問題是什麼。 –
你說得對,這是太多的文字,沒有方向。 – Joris
作爲一個提示,你的主要問題'我認爲這是相當多的代碼,也許可以做得更乾淨.'應該放在帖子的底部而不是中間,所以它可以很容易地找到。 –