1 回答

TA貢獻1801條經驗 獲得超8個贊
該方法必須匹配ParameterizedThreadStartPressBind
的簽名,它接受 type 的參數。在 中,將此參數轉換為字符串。該值在Thread.Start中傳遞給線程。object
PressBind method
另請注意,為了Label
從另一個線程更新文本,您必須使用Invoke。
private void Form1_Load(object sender, EventArgs e)
{
? ? Thread TH = new Thread(PressBind); //I cant make thread for this method
? ? TH.SetApartmentState(ApartmentState.STA);
? ? CheckForIllegalCrossThreadCalls = false;
? ? TH.Start("some-text" /* here you pass the text */);
}
private void TxBxKTB_TextChanged_1(object sender, EventArgs e)
{
? ? TextBox objTextBox = (TextBox)sender;
? ? string text = objTextBox.Text;
? ? label2.Text = $"the bind key is {text}";
? ? PressBind(text);
}
void PressBind(object state)
{
? ? string text = (string)state; // cast object parameter back to string
? ? // do other things...
? ? // must use InvokeRequired + Invoke if accessing Label?
? ? // created by the UI thread
? ? if (InvokeRequired)
? ? {? ??
? ? ? ?Invoke(() => label1.Text = "ready");?
? ? }
? ? else
? ? {
? ? ? ?label1.Text = "ready"; // we're on the UI thread
? ? }
? ? // do other things...
}
- 1 回答
- 0 關注
- 166 瀏覽
添加回答
舉報