WPF BusyIndicator on a background thread

2019-05-25 07:54发布

问题:

I'm using the wpf toolkit busy indicator which provides an overlay on top of my UI while a background operation is taking place. 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. 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? 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();
}

回答1:

I don't believe that you can do that. You can't access UI controls from a background thread. Also, you may find that it is the UI thread that is busy rendering or notifying and making the progress bar freeze.

I have a similar setup in a large scale WPF application and it suffers from the same problem. 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.

I even tried using an animated Gif to get around this issue, but of course they don't animate (by themselves) in WPF applications. 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.

Good luck all the same.



回答2:

No, It is not possible to show the BusyIndicator animations when UI is updating / using Background thread or something.

Refer the related post here. busy indicator during long wpf interface drawing operation

You can show some static image to indicate the busy sign until your UI updation complete.