Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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中的进度条-卡在50%_C#_Wpf - Fatal编程技术网

C# WPF中的进度条-卡在50%

C# WPF中的进度条-卡在50%,c#,wpf,C#,Wpf,我有一个类,它的GUI上有一个BackgroundWorker和一个ProgressBar,如下所示 public class mCrypt { public byte[] DataBlock; public byte[] IVBlock; public static Bitmap FingerprintImg; public byte[] TempX; public byte[] TempAngles; public byte[] TempY; pub

我有一个类,它的GUI上有一个BackgroundWorker和一个ProgressBar,如下所示

public class mCrypt
{
   public byte[] DataBlock;
   public byte[] IVBlock;
   public static Bitmap FingerprintImg;
   public byte[] TempX;
   public byte[] TempAngles;
   public byte[] TempY;
   public byte[] TempKey;
   public byte[] X;
   public byte[] Y;
   public byte[] Angles;
   public byte[] Key;
   public int NoM;

   public mCrypt(BackgroundWorker bw, string imgLoc, string fileLoc, byte[] ivBlock)
   {
      if (!bw.CancellationPending)
      {
         bw.ReportProgress(0);
         LoadImg(imgLoc);
         bw.ReportProgress(7);
         DetectMinutiae(FingerprintImg);
         bw.ReportProgress(25);
         ConvertValues();
         bw.ReportProgress(30);
         LoadFile(fileLoc);
         // This LoadFile method contains DataBlock = File.ReadAllBytes(fileloc);
         bw.ReportProgress(35);
         HandleLength(ivBlock);
         bw.ReportProgress(40);
         ManageInitKey();
         bw.ReportProgress(45);
         GenerateKey();
         bw.ReportProgress(50);
      }
   }

   public byte[] EncryptFile(BackgroundWorker bgw)
   {
      if(!bw.CancellationPending)
      {
         for(int i = 0, i < (DataBlock.Length / 16), i++)
         {
            //Doing cryptographical process here
            ...
            //ProgressBar updates
            if((i / (DataBlock.Length / 16)) + 50 != 100)
               bgw.ReportProgress((i / (DataBlock.Length / 16)) + 50);
            else
               bgw.ReportProgress(100);
         }
      }
   }
}
公共类mCrypt
{
公共字节[]数据块;
公共字节[]IVBlock;
公共静态位图指纹;
公共字节[]TempX;
公共字节[]个角度;
公共字节[]TempY;
公共字节[]临时密钥;
公共字节[]X;
公共字节[]Y;
公共字节[]角度;
公共字节[]密钥;
公共国际名称;
公共mCrypt(BackgroundWorker bw、字符串imgLoc、字符串fileLoc、字节[]ivBlock)
{
如果(!bw.CancellationPending)
{
bw.报告进展(0);
LoadImg(imgLoc);
bw.报告进展情况(7);
检测分钟数(img);
bw.报告进展情况(25);
转换值();
bw.报告进展情况(30);
加载文件(fileLoc);
//此LoadFile方法包含DataBlock=File.ReadAllBytes(fileloc);
bw.报告进展情况(35);
手长(ivBlock);
bw.报告进展情况(40);
ManageInitKey();
bw.报告进展情况(45);
GenerateKey();
bw.报告进展情况(50);
}
}
公共字节[]加密文件(BackgroundWorker bgw)
{
如果(!bw.CancellationPending)
{
对于(int i=0,i<(DataBlock.Length/16),i++)
{
//在这里做加密处理
...
//ProgressBar更新
如果((i/(DataBlock.Length/16))+50!=100)
bgw.报告进度((i/(DataBlock.Length/16))+50);
其他的
bgw.进度报告(100);
}
}
}
}

当我尝试运行应用程序时,ProgressBar只在构造函数运行时更新。将ProgressBar保持在50%状态,直到流程完成。我不明白为什么它不起作用。有什么想法吗?提前感谢

不要直接从
后台工作者
进程中触摸
UI线程
。 使用
Invoke
BeginInvoke
修改
UI

内部工作进程线程:


您没有正确使用
BackgroundWorker
,并且锁定了UI线程

将调用移动到
LoadImg()
LoadFile()
ReportProgress()
进入
DoWork
事件。此外,如果这些方法接触到UI,您可能必须修改它们正在执行的操作


我还建议您阅读以下内容:

您是否已经完成了代码,是否正在执行bgw.ReportProgress调用?这是最糟糕的实现方式。通过使用async/await来实现这一点。我仍然不明白,我应该移动哪些调用?ReportProgress()还是BackgroundWorker?如果我将ReportProgress()移动到DoWork事件中,我应该如何分配百分比?
    bw.DoWork += (sender, args) =>
    {
    Dispatcher.BeginInvoke(DispatcherPriority.Loaded,new Action(()=>
                            {
                                //code that changes progress-bar
                            }
                           ));
     }