Articles in this section
Category / Section

How to cancel asynchronous document loading in SfRichTextBoxAdv control?

1 min read

In SfRichTextBoxAdv control, you can load the document asynchronously using LoadAsync() method. If you are navigating to a new page while the document is loading asynchronously in current page, then the asynchronous load operation should be cancelled properly before navigating to new page.

The following code example demonstrates how to implement cancellation of loading the document in SfRichTextBoxAdv control.


Global declaration of fields in page.

//Represents the document loading task.
Task<bool> loadAsync = null;
//Represents the cancellation token source.
CancellationTokenSource cancellationTokenSource = null;


Loading document in SfRichTextBoxAdv control asynchronously.

if (stgFile != null)
bool showErrorMsg = false;
string errormessage = string.Empty; 
        //Instantiates new cancellation token source.
        cancellationTokenSource = new CancellationTokenSource(); 
        //Invokes the asynchronous load method of SfRichTextBoxAdv.
        loadAsync = this.richTextBox.LoadAsync(stgFile, cancellationTokenSource.Token); 
        await loadAsync; 
    catch(Exception ex) 
        showErrorMsg = true; 
        errormessage = ex.Message; 
    if (showErrorMsg) 
        //Handles the error faced while loading the document in SfRichTextBoxAdv.
        if (!cancellationTokenSource.IsCancellationRequested) 
            MessageDialog dialog = null; 
            if (errormessage.Contains("Access is denied")) 
                dialog = new MessageDialog("This file cannot be open because it is being used by another process"); 
                dialog = new MessageDialog("The input document could not be processed completely, Could you please email the document to for troubleshooting."); 
            await dialog.ShowAsync(); 
//Disposes the cancellation token source
if (cancellationTokenSource != null) 
cancellationTokenSource = null; 
loadAsync = null; 


Cancelling the asynchronous load operation within page unloading event: 

async void DocumentEditor_Unloaded(object sender, RoutedEventArgs e) 
    //Checks if the document load task is executing asynchronously. 
    if (loadAsync != null && !loadAsync.IsCompleted && !loadAsync.IsFaulted && cancellationTokenSource != null) 
        //Invokes the cancel method, to cancel the asynchronous load operation
            //Holds the execution for cancel operation to be completed.
            await loadAsync; 
        { } 
    //Unhooks the events of SfRichTextBoxAdv.
    this.richTextBox.RequestNavigate -= RichTextBoxAdv_RequestNavigate; 
    this.richTextBox.SelectionChanged -= RichTextBox_SelectionChanged; 
    this.richTextBox.ContentChanged -= RichTextBox_ContentChanged; 
    //Disposes the resources used by SfRichTextBoxAdv explicitly. 
    this.richTextBox = null; 


Please find the sample from the following link which demonstrate canceling LoadAsync operation when navigating to another page. 

Sample link:

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied