Integrate RadBusyIndicator with a Service Call in RadWindow
Environment
| Product | Progress® Telerik® UI for WPF |
Description
Show RadBusyIndicator while retrieving data for a RadGridView in a RadWindow. This example uses BackgroundWorker to run the service work on a background thread while the UI displays the busy indicator.
Solution
Create a RadWindow whose content is a RadBusyIndicator. Set the indicator's content to the controls that should remain visible while the service call runs.
RadWindow displaying RadBusyIndicator while data is retrieved

Define the RadWindow
The example defines RadWindow as a user control. See Use RadWindow as a UserControl for details. The XAML below defines the window, busy indicator, grid, and button:
<telerik:RadWindow x:Class="RadWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Header="RadWindow1" Height="500" Width="400">
<Grid>
<telerik:RadBusyIndicator x:Name="radBusyIndicator">
<Grid>
<telerik:RadGridView x:Name="radGridView"
AutoGenerateColumns="True" />
<Button Content="Load Data"
VerticalAlignment="Bottom"
Click="LoadDataButton_Click"
Width="150" />
</Grid>
</telerik:RadBusyIndicator>
</Grid>
</telerik:RadWindow>
Define the Sample Data and Service
The following snippets define sample data and the service that retrieves it:
public class Employee
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public int Age
{
get;
set;
}
public bool Married
{
get;
set;
}
}
Public Class Employee
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set
m_FirstName = Value
End Set
End Property
Private m_FirstName As String
Public Property LastName() As String
Get
Return m_LastName
End Get
Set
m_LastName = Value
End Set
End Property
Private m_LastName As String
Public Property Age() As Integer
Get
Return m_Age
End Get
Set
m_Age = Value
End Set
End Property
Private m_Age As Integer
Public Property Married() As Boolean
Get
Return m_Married
End Get
Set
m_Married = Value
End Set
End Property
Private m_Married As Boolean
End Class
public class EmployeeService
{
public static ObservableCollection<Employee> GetEmployees()
{
ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
Employee employee = new Employee();
employee.FirstName = "Maria";
employee.LastName = "Anders";
employee.Married = true;
employee.Age = 24;
employees.Add(employee); //...
Thread.Sleep(employees.Count * 100);
return employees;
}
}
Public Class EmployeeService
Public Shared Function GetEmployees() As ObservableCollection(Of Employee)
Dim employees As New ObservableCollection(Of Employee)()
Dim employee As New Employee()
employee.FirstName = "Maria"
employee.LastName = "Anders"
employee.Married = True
employee.Age = 24
employees.Add(employee)
'...
Thread.Sleep(employees.Count * 100)
Return employees
End Function
End Class
Run the Service Call in the Background
BackgroundWorker is in the System.ComponentModel namespace. Use its DoWork event to run the time-consuming operation and its RunWorkerCompleted event to hide the busy indicator and display the result:
public partial class RadWindow1
{
private BackgroundWorker worker = new BackgroundWorker();
public RadBusyIndicatorExample()
{
InitializeComponent();
this.radWindow.Show();
worker.DoWork += this.WorkerDoWork;
worker.RunWorkerCompleted += WorkerRunWorkerCompleted;
}
private void WorkerDoWork( object sender, DoWorkEventArgs e )
{
e.Result = EmployeeService.GetEmployees();
}
private void UpdateGridDataSource( ObservableCollection<Employee> employees )
{
this.radGridView.ItemsSource = employees;
this.radBusyIndicator.IsBusy = false;
}
void WorkerRunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
{
Dispatcher.BeginInvoke( new Action<ObservableCollection<Employee>>( this.UpdateGridDataSource ), e.Result );
}
private void LoadDataButton_Click( object sender, RoutedEventArgs e )
{
if ( !worker.IsBusy )
{
this.radBusyIndicator.IsBusy = true;
worker.RunWorkerAsync();
}
}
}
Public Class RadWindow1
Private worker As New BackgroundWorker()
Public Sub New()
' This call is required by the designer.
InitializeComponent()
AddHandler worker.DoWork, AddressOf WorkerDoWork
AddHandler worker.RunWorkerCompleted, AddressOf WorkerRunWorkerCompleted
End Sub
Private Sub WorkerDoWork(sender As Object, e As DoWorkEventArgs)
e.Result = EmployeeService.GetEmployees()
End Sub
Private Sub UpdateGridDataSource(employees As ObservableCollection(Of Employee))
Me.radGridView.ItemsSource = employees
Me.radBusyIndicator.IsBusy = False
End Sub
Private Sub WorkerRunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
Dispatcher.BeginInvoke(New Action(Of ObservableCollection(Of Employee))(AddressOf UpdateGridDataSource), e.Result)
End Sub
Private Sub LoadDataButton_Click(sender As Object, e As RoutedEventArgs)
If Not worker.IsBusy Then
Me.radBusyIndicator.IsBusy = True
worker.RunWorkerAsync()
End If
End Sub
End Class
Open the RadWindow
Create and show the window:
var radWindow = new RadWindow1();
radWindow.Show();
Dim radWindow As RadWindow1 = New RadWindow1()
radWindow.Show()