C# 使用this.invoke和sleep暂时解决UI假死问题,保证background worker.dowork下的线程同步,又不会丢失太多速度
程序员文章站
2022-06-10 16:28:24
...
int intStart = 0;
for (int j = 0; j <= newPaths.Length - 1; j++)
{
File.Copy(newPaths[j], newPaths[j].Replace(sourceFolderName, destFolderName1), true);
File.Copy(newPaths[j], newPaths[j].Replace(sourceFolderName, destFolderName2), true);
Console.WriteLine(newPaths[j]);
Console.WriteLine(newPaths[j].Replace(sourceFolderName, destFolderName1));
Console.WriteLine("---copy to input_pass and output----");
intStart += 1;
//to do: use Invoke to keep sync on this thread, but it will cause the pace slower
if (intStart % 50 == 0) //to do: temperatory solution, still need to deal with the thread
{
this.Invoke(new Action(() =>
{
bgWorker1.ReportProgress(intStart, "copying");
}));
}
System.Threading.Thread.Sleep(1); //to do: temperatory solution, still need to deal with the thread
上一篇: 编写一个asp代码执行器
下一篇: 腌制鸭蛋的办法教给你