Skip to main content

How to create multithreaded chart applications

Getting an application to run smoothly using background threads can really make a big difference.

Unloading non-essential processes to one or more background threads helps keep the application’s UI responsive and fast.

However, compared to a single-threaded program, multithreaded programs need a bit more effort to develop. The careless use of threads is a big reason for subtle bugs, which can take a lot of time to locate.

UI libraries (Windows Forms, WPF, and UWP) are designed in such a way that UI elements may only be accessed from the UI thread. Windows does this to ensure the integrity of UI controls.

Important

LightningChart® .NET control is a UI element. Therefore, the same thread-safe rules apply. Like every other UI control, it requires that all the LightningChart properties should be updated in the UI Thread.

When using a background thread in the application, all UI updates from the thread must go through Invoke (Control.Invoke() in WinForms, and Dispatcher.Invoke() in WPF). Both methods schedule a delegate for execution.

There are 2 amendments in above statement ('all the LightningChart properties should be updated in the UI Thread'). First, if application has multiple windows and each of those windows is allowed to have own thread. In such case LightningChart can use window’s thread not necessary main application thread. Second, LightningChart could be created in Headless Mode, which could be on any background thread. However, if Headless chart is updated, then it should be always from the thread it was created.

For more details and examples of implementation please see our main article.