New to Telerik UI for WPF? Download free 30-day trial

Integrate with services and a RadWindow

This tutorial will show you how to apply a RadBusyIndicator while a long distance calls are being in progress.

One way to achieve this is to make the time consuming service calls in a separate thread. To accomplish this you can use the BackgroundWorker class.

This topic will create a RadWindow control containing a RadGridView and a Button. Pressing the button will reproduce a service call which will retrieve some data. Later when the data is available it will be loaded into the RadGridView and while the call is in progress the RadBusyIndicator will be shown to indicate the ongoing process.

Here is a snapshot of the final result:

WPF RadBusyIndicator Services and RadWindow Integration

  • The first thing you have to do is to declare the RadWindow and set its content to be the RadBusyIndicator content control as long as you want to show the indicator over the RadWindow control.

We will create RadWindow as a user control, the approach is explained in details here.

RadWindow declaration

<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> 

Here is the sample data that is going to be retrieved:

Retrieved sample data

public class Employee 
{ 
    public string FirstName 
    { 
        get; 
        set; 
    } 
    public string LastName 
    { 
        get; 
        set; 
    } 
    public int Age 
    { 
        get; 
        set; 
    } 
    public bool Married 
    { 
        get; 
        set; 
    } 
} 

EmployeeService 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; 
    } 
} 
  • In code you can use the BackgroundWorker to make your service calls in a different thread:

    • Handle the DoWork event to make the time consuming call.

    • Handle the RunWorkerCompleted event to disable the RadBusyIndicator and show the result.

In order to use the BackgroundWorker you will have to add the following using/import in your code:

  • System.ComponentModel

BackgroundWorker usage

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(); 
        } 
    } 
} 
  • Then just open the RadWindow:

Open RadWindow

var radWindow = new RadWindow1(); 
radWindow.Show(); 

Here is the final result:

WPF RadBusyIndicator Services and RadWindow Integration