我找不到答案:
“跨线程操作无效:访问控制'progressBar1' 来自其创建的线程以外的线程。“
这是我的代码:
private void buttonStart_Click(object sender, EventArgs e)
{
ClassCopy cb = new ClassCopy();
cb.startCopy(textBoxSrc.Text, textBoxDest.Text, true);
th = new Thread(loading);
th.Start();
}
private loading()
{
for (int i = 0; i < 100; i++)
{
if (progressBar1.InvokeRequired)
progressBar1.Invoke(new Action(loading));
else
progressBar1.Value = i;
}
}
答案 0 :(得分:0)
这真的只是做
private delegate void EventHandle();
private void loading()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(10);
Invoke(new EventHandle{progressBar1.Value++;})
}
}
睡眠是这样你会看到变化,否则它会在一毫秒内从0变为100
答案 1 :(得分:0)
这个简单的改变将会:
private void loading()
{
for (int i = 0; i < 100; i++)
{
if (progressBar1.InvokeRequired)
progressBar1.Invoke(new Action(loading));
else
progressBar1.Value = i;
}
}