Table of Contents

Client Lifecycle

Constructing, starting, running and stopping StoryboardClient — and what blocks where.

Table of Contents

The Sequence

Construction

Adding Channels

StartStoryboardUI

Starting the Listener

Auto-Start Through the Dispatcher

Shutdown

Desktop Versus Device

The Sequence

sequenceDiagram
    participant App as Application
    participant Client as StoryboardClient
    participant Engine as sbengine

    App->>Client: new StoryboardClient(metadata)
    App->>Client: AddOutputChannel(...)
    App->>Client: AddInputChannel(...)
    Note over App,Client: channels must all be added before starting

    App->>Client: StartStoryboardUI()
    alt development host
        Client->>Engine: start process
    else OpenView device
        Note over Client,Engine: already started by the UI start script
    end
    Client->>Engine: open each channel (blocking, retried)

    App->>Client: StartEventListenerAsync()
    Note over App,Engine: running

    App->>Client: StopEventListener() / Dispose()
    Client->>Engine: terminate process, close channels

Construction

var client = new StoryboardClient(StoryboardMetadata.EventMetadata);

The metadata argument is the generated StoryboardMetadata class. It is what lets message.GetObject<T>() build a typed model from a received message; without it, GetObject<T> returns null.

A second optional argument supplies an IStoryboardAPI implementation. Leave it unset in application code — the client picks the binding for the machine it is running on. Supply one only to substitute a test double.

Note

Constructing the client stores the metadata in a static, so an application that constructs two clients shares one metadata reference. This is fine when both clients were generated from the same event set, which is the normal case even for multi-display applications.

Adding Channels

client.AddOutputChannel("dashboard_ui");   // id 0
client.AddOutputChannel("detail_ui");      // id 1
client.AddInputChannel("multiscreen_backend");

Three rules:

Rule Enforcement
All channels added before starting ApplicationException — "Attempt to add channel after starting client."
Names unique within a direction ApplicationException — "You must specify a unique channel name"
Name not null, empty or whitespace ArgumentException

Ids are assigned per direction, in the order added, starting at zero. The outbound id is what a message's ChannelId must carry to be routed to that engine. Inbound ids exist but are not attached to received messages.

A name longer than 25 characters is logged as a high-severity warning but not rejected — SBIO simply will not create the channel, so it never opens.

StartStoryboardUI

client.StartStoryboardUI();

Does two things:

  1. Starts sbengine — but only on a development host. On an OpenView device the generated UI start script already started it, and starting a second one would give the display two engines.
  2. Opens every channel, which is where this call spends its time.
Important

Opening a channel blocks until the engine creates it, retrying every 100 ms indefinitely. There is no timeout and no failure return. A channel name that matches no Storyboard application therefore hangs startup.

The wait is not silent. After five seconds, and every thirty seconds thereafter, the client logs:

Input Channel thermostat_backend has not opened after 5 seconds.  Verify that
thermostat_backend matches a channel name used by the Storyboard Application and
that the Storyboard Engine is running.

If your application appears to hang on startup, this message is almost certainly in the log. See Troubleshooting.

Calling StartStoryboardUI more than once is safe; subsequent calls return immediately.

Starting the Listener

client.StartEventListenerAsync();

Opens any channels not yet open, then starts a background task that pulls from an internal queue and raises MessageReceived for each event.

Events are queued rather than dispatched inline, so a slow handler delays delivery instead of dropping events or blocking the SBIO callback.

MessageReceived is raised on the listener thread. Marshal to your application's thread before touching shared model state — which is what registering through the dispatcher does for you.

Auto-Start Through the Dispatcher

StoryboardClient implements IDispatchSource and ICanAutoStart, so the dispatcher can own its lifecycle:

Dispatcher.Default.RegisterCallback<StoryboardEventArgs>(HandleInputMessage, client);

// ... register other callbacks, initialise models ...

Dispatcher.Default.StartAndRun();   // opens channels and starts the listener

This is the idiomatic pattern in the demos, and it has two advantages over wiring MessageReceived directly:

  • Handlers run on the dispatcher's thread alongside every other service's callbacks, so model state needs no locking.
  • The dispatcher starts and stops the client with everything else, so shutdown ordering is handled.

With auto-start you still call StartStoryboardUI() yourself — the engine must be running and the channels open before the dispatcher begins pumping. You do not call StartEventListenerAsync().

Shutdown

client.StopEventListener();   // or client.Dispose()

StopEventListener cancels the listener, terminates the engine, closes every channel, and blocks until the listener thread has drained and exited.

Engine termination differs by host:

Host How
Windows Process.Kill() on the process the client started
Everything else pkill -x sbengine

The -x is deliberate: it matches the process name exactly. A looser match (pkill -f) also matches editors, shells and build processes that merely mention sbengine on their command line, and would kill those too on a development machine.

Dispose() closes channels and then calls StopEventListener().

Note

On a device, terminating the engine from the application is usually not what you want — the start script owns it, and the platform restarts the application without restarting the UI. Let the dispatcher's shutdown path run rather than calling StopEventListener directly.

Desktop Versus Device

Development host OpenView device
Engine started by StoryboardClient Generated UI start script
Engine build used storyboardDebugEngineFolder storyboardEngineFolder, copied into the package
Engine running before the app No Yes
StartStoryboardUI() Starts the engine, then opens channels Opens channels only
Channels open quickly After the engine initialises Usually immediately

The client decides by asking whether it is running on a target, not by checking the operating system — a display is also Linux, so the target check has to come first. Desktop Linux falls through to the Ubuntu binding, which does start the engine.

Next: Channels & Messages.