wpf 多线程 对文本框赋值
privatevoidbutton2_Click(objectsender,EventArgse){Threadthread=newThread(CountInTextB...
private void button2_Click(object sender, EventArgs e)
{
Thread thread = new Thread(CountInTextBox);
thread.IsBackground = true;
thread.Start();
// Thread thread2 = new Thread(CountInTextBox);
//thread2.IsBackground = true;
//thread2.Start();
}
object lockKey = new object();
void CountInTextBox()
{
for (int i = 0; i < 1000; i++)
{
//每次执行循环时,都先加对象锁。
lock (lockKey)
{
int num = Convert.ToInt32(txtNum.Text);
num++;
txtNum.Text = num.ToString();//就是这句话 有问题 好像是跨线程的问题 我不会弄
}
}
} 展开
{
Thread thread = new Thread(CountInTextBox);
thread.IsBackground = true;
thread.Start();
// Thread thread2 = new Thread(CountInTextBox);
//thread2.IsBackground = true;
//thread2.Start();
}
object lockKey = new object();
void CountInTextBox()
{
for (int i = 0; i < 1000; i++)
{
//每次执行循环时,都先加对象锁。
lock (lockKey)
{
int num = Convert.ToInt32(txtNum.Text);
num++;
txtNum.Text = num.ToString();//就是这句话 有问题 好像是跨线程的问题 我不会弄
}
}
} 展开
2个回答
展开全部
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询