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 关注
- 132 浏览
添加回答
举报