GetFiles
创建调用的第二个线程CopyFiles
,我只是试图在每次复制文件时用文件名填充列表框,但是一旦代码命中行:
listBox1.Invoke((MethodInvoker)delegate { PrintProgress(i.ToString()); }, new object[] { });
主线程被阻塞,有什么想法吗?
void GetFiles()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(CopyFiles),autoEvent);
//some unrelated code
autoEvent.WaitOne();
}
private void CopyFiles(object stateInfo)
{
for (int i = 0; i < 10; i++)
{
//SetControlPropertyValue(listBox1, i.ToString());
listBox1.Invoke((MethodInvoker)delegate { PrintProgress(i.ToString()); }, new object[] { });
Thread.Sleep(1000);
}
// Signal that this thread is finished.
((AutoResetEvent)stateInfo).Set();
}
private void PrintProgress(string number)
{
listBox1.Items.Add(number);
}