[英]C# how do I disable a button thats running on the main thread?
I have looked everywhere for the answer and I thought it would be simple to find but apparently not. 我到处都在寻找答案,我认为这很容易找到,但显然不是。 I've heard about invoke but I have no idea how to use it or what it is.
我听说过调用,但是我不知道如何使用它或它是什么。 Here is my code:
这是我的代码:
public void Thread1(object sender, EventArgs e)
{
this.button1.Enabled = false;
this.textBox2.Clear();
this.textBox3.Clear();
this.textBox4.Clear();
this.textBox6.Text = "£" + "0";
//Generate 3 random numbers
Stopwatch timer = new Stopwatch();
timer.Start();
this.Refresh();
//This is only part of this function
}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(() => Thread1(sender, e));
Thread newThread = new Thread(threadStart);
newThread.Start();
}
In background threads, use Invoke()
on WinForms components to execute code on the UI thread: 在后台线程中,在WinForms组件上使用
Invoke()
在UI线程上执行代码:
this.Invoke( () => {
this.button1.Enabled = true;
this.textBox2.Text = "whatever";
} );
Documentation: https://msdn.microsoft.com/en-us/library/a1hetckb.aspx 文档: https : //msdn.microsoft.com/en-us/library/a1hetckb.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.