This project has moved. For the latest updates, please go here.

SignalVisualizer: WPF UI thread synchronization

1. Description

The SignalVisualizer module draws a graph of one or more signals which are generated or measured by the test devices. The signals are transmitted from the test device driver to the SignalVisualizer via the loosly coupled event mechanism of CAB. The view of this module consists of Windows Presentation Foundation (WPF) controls.

2. Motivation

The transmission of the signal samples is done over the CAB event broker. The SignalVisualizer registers for this event topic and displays the samples. The event handling has to be synchronized since the SignalVisualizer runs in the UI thread and the signal samples are created by other threads. CAB provides a simple solution for thread synchronization in the event handler as it is shown in Listing 1.

[EventSubscription(EventTopicNames.Sample), Thread = ThreadOption.UserInterface)]
public void GotSample(object sender, SampleEventArgs sample)

Listing 1

The synchronization with the WPF control of the SignalVisualizer does not work properly. During the application shutdown a NullReferenceException is thrown in the System.Windows.Forms.Control.WaitForWaitHandle method. The exception is only thrown if a virtual function generator is not turned off before the application is closed. Even then, the exception does not occur every time. This is a typical behavior for threading issues.

3. Solution

The SignalVisualizer synchronizes the view manually. Listing 2 shows the manual thread synchronization. The code extract is part of the VisualizerView class. The Dispatcher.CheckAccess method verifies if the call needs to be synchronized. Dispatcher.Invoke calls the InnerAddSample method synchronized with the UI thread.

public void AddSample(object device, double amplitude, double relativeTime)
    if (Dispatcher.CheckAccess())
        InnerAddSample(device, amplitude, relativeTime);
        Dispatcher.Invoke(DispatcherPriority.Normal, new AddSampleDelegate(InnerAddSample), device, amplitude, relativeTime);

private void InnerAddSample(object device, double amplitude, double relativeTime)
    signalView.AddSample(device, relativeTime, amplitude);

Listing 2

Note: The communication between test device drivers and the visualizer via loosely coupled events is just exemplarily. Typical, signal samples have to be processed in real time and not in the way it is done in the Test Suite prototype. The event broker is to slow for processing signals with high frequencies.

Last edited Oct 5, 2007 at 4:03 PM by jbe2277, version 3


No comments yet.