BackgroundWorkers in Smart Office Scripts – Part 4

I’m back from Burning Man and ready to write this fourth part of my series on BackgroundWorkers in Lawson Smart Office Scripts to illustrate how to handle worker errors and how to handle exceptions. This article is a continuation of my previous articles, Part 1, Part 2, and Part 3.

How to handle worker errors

When I catch an exception in my programs I want to inform the user about it by displaying an error message in the user interface, whether in a popup or in the status bar, so the user can make eventual corrections and try the action again. But a BackgroundWorker runs in a background thread and doesn’t have access to the user interface, so I cannot inform the user from the method OnDoWork. Instead, I have to let the exception propagate to the method OnRunWorkerCompleted which runs in the UI thread, and inform the user there, whether in a popup or in the status bar. For that, I’ll read e.Error.

Here’s an example:

/* background thread */
function OnDoWork(sender: Object, e: DoWorkEventArgs) {
    doSomethingTimeConsuming();
}

/* UI thread */
function OnRunWorkerCompleted(sender: Object, e: RunWorkerCompletedEventArgs) {
    if (e.Error != null) {
        // error
        e.Error.Message
    } else {
        // success
    }
    ...
}

function doSomethingTimeConsuming() {
     throw new System.Exception('!!!');
}

Note I don’t catch exceptions in the background thread OnDoWork so as to propagate them to the UI thread OnRunWorkerCompleted.

How to handle exceptions

I have to handle exceptions in the Init method and in every event handler: OnRequestingOnRequestedOnProgressChanged, and OnRunWorkerCompleted.

public function Init(element: Object, args: Object, controller: Object, debug: Object) {
    try {
        ...
    } catch (ex: Exception) {
        ...
    }
}

function OnDoWork(sender: Object, e: DoWorkEventArgs) {
    ...
}

function OnRequesting(sender: Object, e: CancelRequestEventArgs) {
    try {
        ...
    } catch (ex: Exception) {
        ...
    }
}

function OnRequested(sender: Object, e: RequestEventArgs) {
    try {
        ...
    } catch (ex: Exception) {
        ...
    }
}

function OnProgressChanged(sender: Object, e: ProgressChangedEventArgs) {
    try {
        ...
    } catch (ex: Exception) {
        ...
    }
}

function OnRunWorkerCompleted(sender: Object, e: RunWorkerCompletedEventArgs) {
    try {
        ...
    } catch (ex: Exception) {
        ...
    }
}

Note I don’t catch exceptions in the background thread OnDoWork so as to propagate them to the UI thread OnRunWorkerCompleted.

Usability

Also, if I have previously disabled the user interface (for example by disabling the Start button) to prevent the user from starting the worker twice, I might want to enable the user interface back when I catch an exception, depending on what the script does, so the user can make eventual corrections and try the action again. Or I can simply let the user press F5 to refresh the screen and start over.

Conclusion

In this article I illustrated how to handle worker errors and how to handle exceptions.

Related articles

All articles in this series:

Published by

thibaudatwork

M3 Technical Consultant

3 thoughts on “BackgroundWorkers in Smart Office Scripts – Part 4”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s