欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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
相关标签: 笔记