Play safe – Use SafeDispatcher – Stop USD Crash

One of the better components that have been released as part of the USD 2.2.1 is ‘SafeDispatcher’. All these days, I hadn’t really given much care or thought to it, but now, after seeing the impact of it practically, I understand how important this component is for all custom hosted control developments. Hence, this blog post.

Note: This blog post is entirely technical and one needs to have some basic understanding of WPF and Custom control development in USD to effectively indulge with it. If you don’t fall in this category, this post might become a bit tedious for you.

Let’s get to know about Dispatcher first. In layman terms, you can consider this as UI Thread. The dispatcher will queue all your code commands into UI thread and will get executed one by one. Until and unless you explicitly mention that this piece of code should run under a separate thread (using Task/Background Worker, etc.), all your code runs under Dispatcher thread by default.

The problem with this Dispatcher/UI thread is that if an unexpected exception occurs in the code which isn’t handled, there is a high chance that your entire application will shut down with a Fatal error. Note that this is the behavior of WPF, not the USD.

So, in case of USD – if one of your custom controls has a problem, imagine how awkward it would be if it crashes the entire USD. How difficult would it be for the agents to start over the USD and go back to the session where they had been working before it crashed? That’s where the SafeDispatcher comes into the picture.

As the name says, it is a ‘Safe’ Dispatcher which can handle all the unhandled exceptions and stops propagating the error further, thereby preventing the USD to crash. In addition to that, it gives a user-friendly message along with full stack trace details for the developers to investigate further.

Here is a quick demo I have created, which I believe will give you a better insight into the topic and will be quick to understand too.

For the sake of this demo, I have created a Global Custom Hosted control. It has 2 buttons: a) Dispatcher, b) Safe Dispatcher. I wrote a couple of methods which throw a Null reference exception, however, one of them is wrapped with Dispatcher, while the other with Safe Dispatcher. Here are some of the screenshots of the code & UI.

Safe Dispatcher Vs Dispatcher

Safe Dispatcher UI

Dispatcher.Invoke

Dispatcher.Invoke method

SafeDispatcher.Invoke

SafeDispatcher.Invoke() method

Now, if I run the application and click on the ‘Dispatcher’ button, you will see that USD has crashed with one big and ugly error message, as shown below:As you can see, there is nothing much in the code. In the first bits, it is a Null object and using GetType() method on Null objects results in Null Reference Exception. This is on UI Thread. In the second piece of code, it is exactly the same, except for the fact that it is SafeDispatcher thread.

USD Crash

Unhanded Exception crashing USD

On the other hand, if you click on ‘Safe Dispatcher’ button, SafeDispatcher component will handle it and will display a nice user-friendly message. Moreover, it will not crash your USD. Here is what the message looks like:

Exception Handled by SafeDispatcher

Exception Handled by SafeDispatcher

I hope this demo helps you understand the importance of this SafeDispatcher component in your code.

So, Developers, please, please make sure that you use SafeDispatcher in all places, wherever you are dealing with UI components, and make USD a good application to work with.

All the very best, Happy coding!!

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