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: OnRequesting, OnRequested, OnProgressChanged, 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:
- BackgroundWorkers in Smart Office Scripts – Part 1 – How to set input parameters, and how to receive output values
- BackgroundWorkers in Smart Office Scripts – Part 2 – How to disable/enable the user interface, how to indicate activity, and how to show progress
- BackgroundWorkers in Smart Office Scripts – Part 3 – How to handle worker cancellation
- BackgroundWorkers in Smart Office Scripts – Part 4 – How to handle worker errors and how to handle exceptions
3 thoughts on “BackgroundWorkers in Smart Office Scripts – Part 4”