Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF进度条未显示正确的进度_C#_Wpf - Fatal编程技术网

C# WPF进度条未显示正确的进度

C# WPF进度条未显示正确的进度,c#,wpf,C#,Wpf,我有一个应用程序,我上传的文件块。我的前端是WPF,我有一个进度条来显示文件上传进度(上传由单独的线程完成,并且进度条是在上传开始时由子线程调用的单独形式) 我在文件中找到了设置进度条最大属性的块总数 现在,对于上传的每个块,我将进度条的值增加1 但令我惊讶的是,进度条开始增加,但从未完成(它在几个块后停止显示进度) 以下是负责上载文件的线程的代码: System.Threading.Thread thread = new Thread( new ThreadStart(

我有一个应用程序,我上传的文件块。我的前端是WPF,我有一个进度条来显示文件上传进度(上传由单独的线程完成,并且进度条是在上传开始时由子线程调用的单独形式)

我在文件中找到了设置进度条最大属性的块总数

现在,对于上传的每个块,我将进度条的值增加1

但令我惊讶的是,进度条开始增加,但从未完成(它在几个块后停止显示进度)

以下是负责上载文件的线程的代码:

System.Threading.Thread thread = new Thread( new ThreadStart( delegate() { // show progress bar - Progress is the name of window containing progress bar Progress win = new Progress(); win.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; win.Show(); // find number of blocks long BlockSize = 4096; FileInfo fileInf = new FileInfo(filename); long FileSize = fileInf.Length; long NumBlocks = FileSize / BlockSize; //set the min and max for progress bar win.Dispatcher.Invoke( new Action( delegate() { win.progressBar1.Minimum = 0; win.progressBar1.Maximum = NumBlocks; } ), System.Windows.Threading.DispatcherPriority.Render); //upload file while (true) { // code to upload the file win.Dispatcher.Invoke( new Action( delegate() { win.progressBar1.Value += 1; } ), System.Windows.Threading.DispatcherPriority.Render); } } System.Threading.Thread Thread=新线程( 新ThreadStart( 代表() { //显示进度条-进度是包含进度条的窗口的名称 进步赢=新的进步(); win.WindowStartupLocation=System.Windows.WindowStartupLocation.CenterScreen; win.Show(); //查找块数 长块大小=4096; FileInfo fileInf=新的FileInfo(文件名); long FileSize=fileInf.Length; long NumBlocks=文件大小/块大小; //设置进度条的最小值和最大值 win.Dispatcher.Invoke( 新行动( 代表() { win.progressBar1.Minimum=0; win.progressBar1.Maximum=NumBlocks; } ),System.Windows.Threading.DispatcherPriority.Render); //上传文件 while(true) { //上传文件的代码 win.Dispatcher.Invoke( 新行动( 代表() { win.progressBar1.Value+=1; } ),System.Windows.Threading.DispatcherPriority.Render); } } 有人能帮我分析一下为什么会这样吗

谢谢。

问题是:

上传由单独的线程完成,并且 进度条采用单独的形式 在以下情况下由子线程调用 上传开始

如果这意味着您的子线程创建了表单,那么这就是问题所在。您的子线程可能正在更新进度条值,但这只会使显示无效,而不一定刷新显示。当控件的显示无效时,它只会记录下一次有机会时必须重新绘制它的显示。a刷新是控件实际渲染到屏幕的时间

更好的方法是在主线程中创建进度条表单

然后,工作线程可以更新状态,主线程将刷新显示

需要记住的一点是:如果要更新在不同线程中创建的控件,则必须通过该控件的调度程序进行更新

var dispatcher = progressBar.Dispatcher;
dispatcher.BeginInvoke(new Action( () => { progressBar.Value = currentProgress }));

查看代码后编辑

您所需要做的就是移动progress变量的创建,以便在创建工作线程之前由主线程实例化它



你希望人们在没有任何代码的情况下调试你的代码吗?很抱歉,我将从下次开始记住,谢谢你可以编辑你的问题并添加代码。你应该这样做。你能详细说明“使显示无效,而不必刷新显示”的问题吗…我没有得到那个角色,谢谢。谢谢你,你帮了我大忙!!:)
Progress win = new Progress();
win.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
win.Show();
System.Threading.Thread thread = new Thread(
   new ThreadStart(
         delegate()
         {
 // ...