简体   繁体   English

WPF BusyIndi​​cator在后台线程上

[英]WPF BusyIndicator on a background thread

I'm using the wpf toolkit busy indicator which provides an overlay on top of my UI while a background operation is taking place. 我正在使用wpf工具箱繁忙指示器,该指示器在进行后台操作时在UI顶部提供覆盖。 There's a progress bar in the control which is set to indeterminate which is fine while my background task is going on. 控件中有一个进度条,它设置为不确定,这在我执行后台任务时很好。 Once the background task is complete, the UI needs to update which can take 1-2 seconds. 后台任务完成后,UI需要更新,这可能需要1-2秒。 This of course causes the progress bar to freeze which looks ugly. 当然,这会使进度条冻结,看起来很难看。

My question is, how can I spin up the busy indicator on a background thread so that the progress bar carries on moving all the way up until the UI becomes responsive? 我的问题是,如何旋转后台线程上的忙碌指示器,以便进度条一直向上移动直到UI响应为止? I'm open to other solutions as well as long as the progress bar doesn't freeze. 我对其他解决方案也很开放,只要进度条不会冻结即可。

Here's some sample code: 这是一些示例代码:

<xctk:BusyIndicator IsBusy="{Binding IsBusy}" Style="{StaticResource BusyIndicatorStyle}">
    <DockPanel Margin="3">
        <TextBlock DockPanel.Dock="Top" Style="{StaticResource WorkspaceHeaderStyle}" Text="User Management"/>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ToolBar Grid.Row="0">
                <Button Content="Save changes" Command="{Binding SaveChangesCommand}"/>
            </ToolBar>
            <TabControl Grid.Row="1">
                <TabItem Header="Users" DataContext="{Binding UsersViewModel}">
                    <users:UsersView />
                </TabItem>
                <TabItem Header="Roles" DataContext="{Binding RolesViewModel}">
                    <roles:RolesView />
                </TabItem>
            </TabControl>
        </Grid>
    </DockPanel>
</xctk:BusyIndicator>
private void LoadDays()
{
    ProgressIsBusy = true;

    var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

    var loadDaysTask = GetLoadDaysTask(uiScheduler);

    loadDaysTask.ContinueWith(antecedent =>
    {
        RaisePropertyChanged(() => ForecastViewModel);
        RaisePropertyChanged(() => AverageHandleTimeViewModel);
        RaisePropertyChanged(() => GeneralOptionsViewModel);
        RaisePropertyChanged(() => ScheduledHoursViewModel);        

        IsUserEditing = true;
        ProgressIsBusy = false;

    }, TaskScheduler.FromCurrentSynchronizationContext());

    loadDaysTask.Start();
}

I don't believe that you can do that. 我不相信你可以做到。 You can't access UI controls from a background thread. 您无法从后台线程访问UI控件。 Also, you may find that it is the UI thread that is busy rendering or notifying and making the progress bar freeze. 另外,您可能会发现正是UI线程忙于渲染或通知进度条并使其冻结。

I have a similar setup in a large scale WPF application and it suffers from the same problem. 我在大型WPF应用程序中具有类似的设置,并且遇到相同的问题。 The busy indicator displays fine while data is being fetched from the database, but then when the application starts to render the data in the UI, the busy indicator freezes. 从数据库中获取数据时,忙碌指示器显示正常,但是当应用程序开始在UI中呈现数据时,忙碌指示器冻结。

I even tried using an animated Gif to get around this issue, but of course they don't animate (by themselves) in WPF applications. 我什至尝试使用动画Gif来解决此问题,但是当然,它们在WPF应用程序中不会(单独)设置动画。 Having written the code to animate the frames in the Gif, I was very disappointed to find that it also suffered from the same problem. 编写了用于在Gif中对帧进行动画处理的代码后,我很失望地发现它也遇到了相同的问题。

Good luck all the same. 祝你好运。

No, It is not possible to show the BusyIndicator animations when UI is updating / using Background thread or something. 不,当UI正在更新/使用背景线程之类的东西时,无法显示BusyIndi​​cator动画。

Refer the related post here. 请参阅此处的相关文章。 busy indicator during long wpf interface drawing operation 长WPF界面绘制操作期间的忙碌指示器

You can show some static image to indicate the busy sign until your UI updation complete. 您可以显示一些静态图像来指示繁忙的信号,直到完成UI更新为止。

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

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