Multithreading Real-Time Chart

Real-Time Chart Example in C++ (MFC, Qt) and C# (.NET Windows Forms, WPF)

Multithreading Real-Time Chart in C++ (MFC, Qt) and C# (.NET) This example demonstrates a real-time chart in which the data are acquired from a separate thread. It is based on the Real-Time Chart with Zooming and Scrolling sample code in the ChartDirector distribution, and is available in C++ (MFC, Qt) and C# (.NET Windows Forms, WPF).

The following only explains the multithreading part of the code. For how to create real-time charts with zooming and scrolling interface, programmable track cursors, and save snapshots in PDF/SVG/PNG/JPG/BMP, please refer to the documentation of the original Real-Time Chart with Zooming and Scrolling sample code.

Real-Time Charts and Multithreading

Many real-time chart applications requires data to be acquired regularly in high speed. However, the GUI timers are inaccurate as the GUI thread is affected by user interactions. For example, on Windows, right clicking on the window title bar can cause the GUI message loop to halt until the mouse button is released. For accurate timing, real-time data acquisition often needs to run in a separate thread.

GUI components are constantly invoked by the operating system to handle system messages, such as those for mouse and paint events. That means a GUI component should not be accessed from another thread, such as the data acquisition thread, as this may result in uncoordinated concurrent access, and can cause strange errors and random crashes.

In this example, data are acquired in a data acquisition thread and stored in a special "double buffered queue". The GUI thread periodically reads data from the queue and updates the real-time chart. This is illustrated below.
The "double buffered queue" is thread-safe and is designed so that reading and writing always work on different buffers. This allows read and write to occur concurrently with minimal contention. The functions of the queue are:

Download

*** Note *** : If you are new to ChartDirector, it is recommended you download ChartDirector from the download page to try it. It includes complete ChartDirector documentation as well as plenty of sample code designed as tutorials. The following assumes you already have some basic understanding of ChartDirector.
The sample code in this page is included as part of the "Real-Time Chart Extra Sample Code Pack".
Real-Time Chart Extra Sample Code Pack for C++ (MFC, Qt)
Real-Time Chart Extra Sample Code Pack for .NET (C# Windows Forms, WPF)

Reference

The followings are links to the documentation of the original Real-Time Chart with Zooming and Scrolling sample code.