Using WaitingBar with a Background Worker
RadWaitingBar is a useful control for indicating that a long-running operation is undergoing. When using this control, however, many users face a similar issue: once the time-consuming operation is started, the control does not move its indicators and literally freezes. Such cases occur when the long-running operation is executed on the same thread as the RadWaitingBar waiting process: the primary UI Thread. The operation does not allow the form to update its UI and as a result the control does not perform any waiting animation.
One obvious solution is to start the time-consuming operation in a new thread. The following example illustrates how to achieve this through a BackgroundWorker.
BackgroundWorker solution
The aim of the sample application is to calculate numbers of the Fibonacci sequence. In a straight-forward scenario, the user selects the position of the Fibonacci number through a RadSpinEditor and clicks the Start RadButton to trigger the time-consuming operation. While the calculations are undergoing the RadWaitingBar smoothly animates its waiting indicators and the RadForm remains responsive. Once the number is calculated, the result is displayed and the RadWaitingBar control is stopped. Below you will find snippets and comments which provide a detailed description of the sample application.
Fig.1 Fibonacci example
1. When the form is loaded the BackgroundWorker instance should be initialized. Additionally, you should subscribe to two of its events: DoWork and RunWorkerCompleted.
2. When the user clicks the Start RadButton, you should run the BackgroundWorker through the RunWorkerAsync method and, also, start the RadWaitingBar waiting process using the StartWaiting method.
Fig.2 Calculation in progress
3. In the DoWork event handler you should execute the time-consuming operation, i.e. calculate the required Fibonacci number.
If you want to interupt the long-lasting operation, you can click the Cancel button where the BackgroundWorker.CancelAsync method is called.
4. When the long-running operation has completed, you should stop the RadWaitingBar control waiting process through the StopWaiting method. Additionally, you should display the result to the user.
Fig.3 Result