WPF
Loading Animation
User Interface
Windows Presentation Foundation
UI Design

WPF loading animation (spinner) that keeps the UI responsive

Codemia Knowledge Hub ·

A WPF loading spinner is easy to draw with a Storyboard. The part that matters more is keeping the UI thread free while the real work runs, because blocking it freezes the animation you just built.

A simple spinner with a Storyboard

A small rotating shape is enough for a clean loading indicator:

xml
1<Window x:Class="LoadingDemo.MainWindow"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4        Title="Loading Demo" Height="220" Width="320">
5    <Window.Resources>
6        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
7    </Window.Resources>
8    <Grid>
9        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
10            <Border Width="40" Height="40"
11                    BorderThickness="4"
12                    BorderBrush="DodgerBlue"
13                    CornerRadius="20"
14                    Visibility="{Binding IsLoading, Converter={StaticResource BoolToVisibilityConverter}}">
15                <Border.RenderTransform>
16                    <RotateTransform x:Name="SpinnerTransform" CenterX="20" CenterY="20" />
17                </Border.RenderTransform>
18                <Border.Triggers>
19                    <EventTrigger RoutedEvent="Loaded">
20                        <BeginStoryboard>
21                            <Storyboard RepeatBehavior="Forever">
22                                <DoubleAnimation
23                                    Storyboard.TargetName="SpinnerTransform"
24                                    Storyboard.TargetProperty="Angle"
25                                    From="0" To="360" Duration="0:0:1" />
26                            </Storyboard>
27                        </BeginStoryboard>
28                    </EventTrigger>
29                </Border.Triggers>
30            </Border>
31
32            <Button Content="Start Work" Width="120" Margin="0,20,0,0" Click="StartWork_Click"/>
33        </StackPanel>
34    </Grid>
35</Window>

The key piece is the continuously animated RotateTransform.

Run the real work asynchronously

If the click handler does heavy work on the UI thread, the animation stops even though the XAML is correct. Use async and await the background work:

csharp
1using System.ComponentModel;
2using System.Runtime.CompilerServices;
3using System.Threading.Tasks;
4using System.Windows;
5
6namespace LoadingDemo;
7
8public partial class MainWindow : Window, INotifyPropertyChanged
9{
10    private bool _isLoading;
11
12    public bool IsLoading
13    {
14        get => _isLoading;
15        set
16        {
17            _isLoading = value;
18            OnPropertyChanged();
19        }
20    }
21
22    public MainWindow()
23    {
24        InitializeComponent();
25        DataContext = this;
26    }
27
28    private async void StartWork_Click(object sender, RoutedEventArgs e)
29    {
30        IsLoading = true;
31        try
32        {
33            await Task.Run(async () =>
34            {
35                await Task.Delay(3000);
36            });
37        }
38        finally
39        {
40            IsLoading = false;
41        }
42    }
43
44    public event PropertyChangedEventHandler? PropertyChanged;
45
46    private void OnPropertyChanged([CallerMemberName] string? name = null)
47    {
48        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
49    }
50}

The slow work happens off the UI thread, so the UI thread stays free to animate.

Why a frozen spinner is a threading problem

WPF renders and updates UI elements on the UI thread, and a loading animation is no exception. The spinner cannot update smoothly while your code runs any of these on that thread:

  • synchronous network calls
  • large file reads
  • long CPU loops
  • blocking sleeps

Show the spinner only while work is running

The indicator should reflect a real state change:

  • show it when work begins
  • hide it when work completes or fails
  • optionally replace it with a success or error message

Users lose trust in a spinner that keeps going after the task has finished or deadlocked. Clearing the flag in a finally block, as above, covers the failure path too.

Binding IsLoading from a view model

In larger WPF applications, keep the loading state in a view model and bind Visibility or IsEnabled to it. The animation stays in XAML while the view model exposes IsLoading. That separates visual behavior from application logic and keeps code-behind small.

A simple spinner with correct async behavior usually beats a flashy control on a blocked UI thread. If the spinner freezes, look for blocking code on the UI thread first.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms