If you have multiple charts and you want them to operate independently (i.e. multiple UI threads with their own dispatchers) take a look at this Visual Studio 2010 solution. Each chart has its own window and dispatcher object. This way updating one chart won't block the other charts from being rendered. Also, blocking the main window UI thread won't affect rendering of the other windows/charts.
When you start the application, it creates four additional windows that contain one chart. To make this simpler, data for each chart is generated using a DispatcherTimer instance on each window. Each window has a "Sleep for 5 seconds" button that blocks the corresponding UI thread for 5 seconds using Thread.Sleep method. The button is there to demonstrate the asynchronous operating of each window. When you click the button on one window, rendering is blocked for 5 seconds but the other windows won't be affected by this. Rendering resumes after 5 seconds. The main window has also text that changes its size to visually show when the main windows' UI thread is not blocked.
If you want to create a new chart window, just click "Open new chart windows" button. It will create a new instance of ChartWindow class which the starts to run on its own UI thread.
To demonstrate how to make changes to other charts from a different UI thread there's also a "Add marker to all charts" button. It will add one marker to all available charts with the same timestamp. It uses a ThreadPool thread to create a marker for each chart and adding the marker is synchronized to the corresponding chart's UI thread using Dispatcher.Invoke method. Using ThreadPool in this case is recommended because if one of the chart windows' UI thread is blocked, the main window will wait until the block is cleared, hence blocking the main window's UI thread. Now the ThreadPool thread will wait and the main window UI thread can run normally.
Adding a marker is implemented just to demonstrate how to update a chart that runs in an independent UI thread. Same principle can be applied to other chart related operations (adding data, setting axis range, etc...).
Please note that each window run on its own UI thread so closing the main window won't close the application. You will need to close all the windows by hand or implement a common window closing functionality - for example in the main window's Closing/Closed event handler.
Also, using multiple UI threads is only possible with separate windows. Placing each chart inside the same parent (e.g. UserControl) will require each chart to be owned by the same UI thread as the parent. You will get the "The calling thread cannot access this object because a different thread owns it." exception.
Multiple charts and multiple UI threads
Moderator: Queue Moderators
Multiple charts and multiple UI threads
- Attachments
-
- MultipleAsyncChartWindows.zip
- (8.04 MiB) Downloaded 1858 times
-
- screen.png (206.98 KiB) Viewed 33357 times
Re: Multiple charts and multiple UI threads
Supposedly multithreaded UI is also possible in the same window by using the HostVisual class. The only drawback is that you lose interaction with the chart. Perhaps you can create an example for that as well?
- ArctionNikolai
- Posts: 38
- Joined: Fri Feb 05, 2016 11:37 am
- Location: Finland
- Contact:
Re: Multiple charts and multiple UI threads
Hello,jrvdboom wrote:Supposedly multithreaded UI is also possible in the same window by using the HostVisual class. The only drawback is that you lose interaction with the chart. Perhaps you can create an example for that as well?
We plenty of examples, in WinForms and WPF demo applications, e.g. ExampleThreadMultiChannel example. If something intends to update a chart, it should be updated in UI thread using Dispatcher.Invoke in WPF or Control.Invoke in WF. In Invoke method you call your delegate method, which is responsible for chart update. Ensure that background threads are okay and not updating chart without Invoke.
Best regards,
Nikolai
Nikolai Arsenov
Software developer
Arction Ltd
Microkatu 1, 70210 Kuopio, Finland
Software developer
Arction Ltd
Microkatu 1, 70210 Kuopio, Finland
- ArctionPasi
- Posts: 1367
- Joined: Tue Mar 26, 2013 10:57 pm
- Location: Finland
- Contact:
Re: Multiple charts and multiple UI threads
Here's the updated example code for v.8.
LightningChart Support Team, PT