# Integrate RadBusyIndicator with a Service Call in RadWindow

## Environment

<table>
    <tr>
        <td>Product</td>
        <td>Progress® Telerik® UI for WPF</td>
    </tr>
</table>

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

> caption RadWindow displaying RadBusyIndicator while data is retrieved

![RadBusyIndicator and RadWindow integration](https://www.telerik.com/products/wpf/documentation/assets/b0195e0249513f6b3aaf2c2bccb3b9a1/radbusyindicator_how_service_and_radwindow_010.png)

### Define the RadWindow

The example defines `RadWindow` as a user control. See [Use RadWindow as a UserControl](https://www.telerik.com/products/wpf/documentation/controls/radwindow/how-to/use-radwindow-as-user-control) for details. The XAML below defines the window, busy indicator, grid, and button:

```XAML
 	<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:

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

```VB.NET
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
```

```C#
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;
    }
}
```

```VB.NET
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:

```C#
    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();
            }
        }
    }
```

```VB.NET
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:

```C#
    var radWindow = new RadWindow1();
    radWindow.Show();
```

```VB.NET
Dim radWindow As RadWindow1 = New RadWindow1()
radWindow.Show()
```

## See Also

* [RadBusyIndicator Getting Started](https://www.telerik.com/products/wpf/documentation/controls/radbusyindicator/getting-started)
* [Progress Determination](https://www.telerik.com/products/wpf/documentation/controls/radbusyindicator/features/progress-determination)
* [Report Progress Value](https://www.telerik.com/products/wpf/documentation/controls/radbusyindicator/features/report-progress-value)
* [Custom Busy Content](https://www.telerik.com/products/wpf/documentation/controls/radbusyindicator/features/custom-busy-content)
