简体   繁体   English

进度栏将无法完成

[英]Progress Bar will not complete

I have a worker, and a progress bar. 我有一个工人,还有一个进度栏。 progress bar is in the View and the worker is in the model. 进度条在视图中,工作器在模型中。 I want to show progress bar to the user so they know how long they have to wait. 我想向用户显示进度条,以便他们知道他们需要等待多长时间。 for all results to finish populating. 完成所有结果的填充。

Worker thread looks like this 工作线程看起来像这样

Model 模型

ManagementObjectSearcher MetaClasses = new ManagementObjectSearcher(CurrentScope, new SelectQuery("SELECT * FROM meta_class"));
int index = 0;
int Total = MetaClasses.Get().Count;
foreach (ManagementObject Metaclass in MetaClasses.Get())
{
    try
    {
        index++;
        BGW.ReportProgress((index * 100) / Total, Metaclass["__CLASS"].ToString());
    }
    catch { }
    Thread.Sleep(100);
}

the worker sends the information to the ProgressUpdated method, i did this so the UI updated with the availible data for the end user. 工作人员将信息发送到ProgressUpdated方法,我这样做是为了使UI用最终用户的可用数据更新。

Model 模型

public void Workers_Changed(object sender, ProgressChangedEventArgs e)
{
    Progress = e.ProgressPercentage;
    switch (TaskToRun)
    {
        case "NameSpace":
            {
                BaseClasses.Add(e.UserState.ToString());
                if (Progress == 100) { Thread.Sleep(200); }
                break;
            }
        case "Class":
            {
                return;
            }
    }
}

Again Works perfectly. 再次完美。 My progress bar Value is bound as well. 我的进度栏值也受约束。

XAML XAML

<ProgressBar Height="20" Name="ProgressBar_WMI"
             Value="{Binding Progress,Mode=OneWay}"
             DockPanel.Dock="Top"
             Minimum="1"
             Maximum="100" Foreground="Red"  ValueChanged="ProgressBar_WMI_ValueChanged" />

Issue: when the progressbar hits about 95% it doesn't show as completed in the UI thread 问题:进度条命中率达到95%时,它在UI线程中未显示为已完成

View (Xaml.CS) 查看(Xaml.CS)

public void HideProgressBar()
{
    Thread.Sleep(3000);
    ProgressBar_WMI.Visibility = Visibility.Collapsed;
}

public void ProgressBar_WMI_ValueChanged(object sender, RoutedEventArgs e)
{
    if (ProgressBar_WMI.Value == ProgressBar_WMI.Maximum)
    {
        ProgressBar_WMI.Foreground = Brushes.Green;
        HideProgressBar();
    }
    else {
        ProgressBar_WMI.Foreground = Brushes.Red;
        ProgressBar_WMI.Visibility = Visibility.Visible;
    }
}

My idea is to have the bar hit to 100% completed and turn green to indicate that its completed and the disappear/collapse after 3 seconds. 我的想法是使条形图达到100%,然后变绿以指示其完成,并在3秒后消失/塌陷。

Any Idea's? 有任何想法吗?

I can see two possible reasons for your ProgressBar not reaching 100%. 我可以看到您的ProgressBar未达到100%的两个可能原因。 The first is simply because you might be hiding it as soon as it reaches that point and so, you never actually see it. 首先是因为您可能会在到达该点时立即将其隐藏,因此您实际上从未看到过它。

The other possible reason is that this calculation never reaches your maximum limit value: 另一个可能的原因是此计算从未达到您的最大限值:

BGW.ReportProgress((index * 100) / Total, Metaclass["__CLASS"].ToString());

Only you can verify that, but you might be losing some precision in the actual calculation by using int s here... I am aware that the ReportProgress method takes an int 只有你可以验证,但你可能通过使用失去实际计算较为精确int下面就...... 知道, ReportProgress方法需要一个int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM