System designHow-to

Grab images from multiple cameras in .NET: one wait, four frames

Grab multiple cameras in .NET without serialising them: start each stream, await all waits with Task.WhenAll, and push the four images to WPF in one pass.

LumaSpec Vision10 min read Share on LinkedIn

On this page 9 sections
  1. 01Why does a blocking grab loop slow four cameras down?
  2. 02How do you wait for all streams with one await?
  3. 03What must each camera have before the wait works?
  4. 04How much cycle time do four cameras actually need?
  5. 05How do you put four frames on screen as one composite?
  6. 06What happens when one camera misses the trigger?
  7. 07How do you wire the pattern into your trigger loop?
  8. 08Which hardware keeps four triggered cameras on time?
  9. 09Questions

Four GigE area scan cameras fire on one PLC trigger, and the four frames are one measurement: one composite image to analyse, four views to show. The usual first attempt, a BackgroundWorker per camera with a blocking wait, quietly serialises the set and eats the trigger period. The fix is structural. Each camera owns a stream whose acquisition already runs in parallel inside its ring buffer. Your code only has to start one asynchronous wait per stream and await them together with Task.WhenAll. This guide walks through the pattern in C# and WPF: what each camera needs before the wait works, what four 6 MP frames cost on GigE, how to bound the wait when a camera misses the trigger, and how to land all four images on screen in one pass.

Why does a blocking grab loop slow four cameras down?

A blocking loop turns four parallel cameras into one serial pipeline. The loop waits out camera 1's transfer before camera 2's wait starts. At about 55 ms per camera, the fourth image lands roughly 165 ms after the first — a third of a 500 ms cycle gone.

The delay is not the cameras' fault. A machine-vision driver acquires continuously into each camera's ring buffer while your code does something else, which is what the buffer is for: acquisition already runs in parallel across the four devices. The serialisation lives entirely in where you wait. Four BackgroundWorkers add four threads, four exception paths and a fifth join you write by hand, and none of it moves a single frame faster.

If you are porting from a Python prototype that grabbed all four frames on one thread, the hardware trigger grab loop in Python shows the single-camera version of that loop. The .NET version differs mainly in that the wait can be awaited instead of blocked on.

How do you wait for all streams with one await?

Build one asynchronous wait task per stream, materialise the set with ToArray(), and hand it to Task.WhenAll. With a 500 ms trigger period the composite completes tens of milliseconds after the trigger, bounded by the slowest camera rather than the sum of all four.

// Example names: CameraStream stands for your SDK's stream object and
// WaitAsync() for its asynchronous wait. One started stream per camera.
List<CameraStream> streams = new List<CameraStream>
{
    streamCam1, streamCam2, streamCam3, streamCam4
};

// Materialise the task array once: WhenAll enumerates the collection,
// and re-enumerating a lazy Select later would start NEW waits.
Task<Image>[] imageTasks = streams
    .Select(stream => stream.WaitAsync())
    .ToArray();

await Task.WhenAll(imageTasks);

// Every task is completed here, so .Result returns immediately:
// the four frames, in stream order.
Image[] frames = imageTasks.Select(task => task.Result).ToArray();

Two details make this hold up in practice. First, call ToArray() immediately: Task.WhenAll enumerates the collection once to start the waits. If you enumerate the same lazy Select again afterwards you call the wait method a second time and start fresh waits on new frames. Second, read each frame from its own task's Result property after the await. The Task.WhenAll overload for non-generic tasks returns no results of its own.

If each composite must be tied to the PLC cycle that produced it — a counter value read alongside the trigger, for instance — put a reference queue between trigger and await. The match async capture calls to grab results article shows that queue pattern.

What must each camera have before the wait works?

Give every camera its own acquisition and stream object, start each stream once at start-up, and keep the ring buffer two or three frames deep. Wire the PLC trigger to each camera's trigger input; the triggered frame then sits in the buffer until your wait picks it up.

One acquisition object per camera and per process is the root rule. Sharing an acquisition across devices is behind most "second camera hangs" reports, and the multiple GenTL cameras in one process article covers that set-up. Buffer depth two or three absorbs processing that occasionally overruns the period: at a 500 ms cycle with sub-100 ms processing, depth two already covers a full period of overrun.

If your SDK hands you buffers to requeue by hand, requeue only buffers the driver has retired. Requeueing an in-flight buffer stalls acquisition, which the buffer requeue article demonstrates frame by frame.

How much cycle time do four cameras actually need?

Four 6 MP monochrome frames at 8 bits are 6 MB each. On its own GigE port each transfer takes about 55 ms, so the composite is ready roughly 55 ms after the trigger. Four cameras sharing one 1 GbE uplink stretch that to about 218 ms.

Work it from the wire. A GigE link runs at 1 Gbit/s, which is 125 MB/s at the physical layer, and protocol overhead leaves roughly 110 MB/s sustained for image data. The example camera produces 6,000,000 pixels at one byte per pixel:

frame size = 6,000,000 px × 1 B/px = 6 MB one port = 6 MB ÷ 110 MB/s ≈ 55 ms four ports = 4 × 55 ms, in parallel ≈ 55 ms wall time one uplink = 24 MB ÷ 110 MB/s ≈ 218 ms 12 MP frames, one uplink = 4 × (12 MB ÷ 110 MB/s) ≈ 436 ms

Scale the arithmetic to your sensor. A 12 MP frame doubles the per-camera transfer to about 109 ms, and four of them on a shared uplink take about 436 ms of a 500 ms cycle. That leaves 64 ms for exposure, compositing and the ML pass, which is effectively nothing. One network port per camera, or a switch whose uplink sits inside the budget, is the decision that keeps the pattern honest.

Add exposure to the same budget. A strobed exposure of 1–5 ms barely moves the total; a 50 ms ambient-light exposure pushes the composite past 100 ms and deserves its own line in the plan.

How do you put four frames on screen as one composite?

Finish the await off the UI thread, then marshal to it once and assign all four images to their displays in a single pass. WPF then repaints the four views together instead of dribbling them in across four frame updates.

Where you await decides where the code after it runs. An await resumed on the UI thread — a composite loop started from a button handler, say — puts every line after it on the dispatcher. Assigning the four display properties one after another then happens inside one UI pass. That is exactly what you want for the display and exactly what you do not want for processing, so push the analysis into Task.Run and marshal back.

// Composite loop on a background context; dispatcher captured at start-up.
Image[] frames = imageTasks.Select(task => task.Result).ToArray();

await Task.Run(() => RunInspection(frames));   // RunInspection: your analysis pass

dispatcher.Invoke(() =>
{
    view1.Image = frames[0];   // example assignments: map these to your
    view2.Image = frames[1];   // display control's image property
    view3.Image = frames[2];
    view4.Image = frames[3];
});

One Invoke with four assignments paints the set together; four separate Invoke calls let the UI repaint in between. When the downstream analysis slices the four views into one GPU batch, the multiple camera views on one GPU tensor buffer article shows the per-view slicing.

What happens when one camera misses the trigger?

One missed trigger leaves you with three completed waits and one that never returns. Time each composite at about 1.5 times your trigger period — 750 ms at 500 ms — and read every task's Exception, because Task.WhenAll rethrows only the first fault.

Two failure shapes need different responses. A camera that received no trigger has an empty buffer and its wait never completes, so bound the whole composite with Task.WhenAny against a Task.Delay. A camera whose frame corrupted in transfer completes with a faulted task, and because Task.WhenAll rethrows only the first exception, you walk the task array and read each task's Exception (an AggregateException) to see every camera's failure rather than camera 1's alone.

TimeSpan compositeTimeout = TimeSpan.FromMilliseconds(1.5 * 500);   // 750 ms at a 500 ms period

Task composite = Task.WhenAll(imageTasks);
bool arrived = await Task.WhenAny(composite, Task.Delay(compositeTimeout)) == composite;

for (int i = 0; i < imageTasks.Length; i++)
{
    if (!imageTasks[i].IsCompleted)
        Report(i, "no frame within the composite timeout");   // Report: example flag per camera
    else if (imageTasks[i].IsFaulted && imageTasks[i].Exception != null)
        foreach (Exception inner in imageTasks[i].Exception.InnerExceptions)
            Report(i, inner.Message);
}

A firing timeout tells you that, not why. Check the trigger line first, then the camera's transfer statistics: corrupted frames look exactly like latency, stutter and "sometimes slow" composites. The hardware trigger grab timeout article shows how to interrupt the wait cleanly instead of leaking a dangling task every cycle.

How do you wire the pattern into your trigger loop?

Set the pattern up once at start-up and repeat five moves per trigger. Wait all streams in one await, bound the wait, check every task, process off the UI thread, update the displays in one pass. Measured trigger-to-composite time for the 6 MP example is about 60 to 120 ms.

  1. At start-up, open each camera, create its own acquisition and stream, set the trigger input to the PLC signal, and start the stream once.
  2. Size each ring buffer to two or three frames and confirm the camera is in trigger mode, not free-run.
  3. Per trigger, start one asynchronous wait per stream and call ToArray() on the resulting set immediately.
  4. Bound the composite with Task.WhenAny against Task.Delay at 1.5 times the trigger period.
  5. Walk every task: not completed means no trigger or no frame; faulted means read that camera's AggregateException.
  6. Composite and process inside Task.Run, then marshal once to the UI thread and assign the four displays in one Invoke.
  7. Verify each cycle with a stopwatch from trigger edge to composite, four frame IDs advanced by exactly one, and zero transfer errors in your SDK's statistics view.

The recurring mistakes, roughly in order of how often they cost a day: enumerating the lazy Select twice, which starts new waits; blocking .Result on the UI thread; sharing one acquisition object between cameras; leaving one camera in free-run; and blaming acquisition latency before checking transfer statistics.

Which hardware keeps four triggered cameras on time?

Give each camera its own NIC port instead of sharing one link. Five or more LAN ports on the host keep four simultaneous 6 MB transfers at about 55 ms, and PoE camera ports remove the per-camera power supply as well.

Two camera ends that both fit the pattern:

Area scan camerasHikrobot MV-CU020-90GC 2.5 MP Area Scan Camera, GigE, GMAX4002, ColorMV-CU020-90GC
Sensor format
1/1.7"
Lens mount
C-mount
$309.00View part
Area scan camerasHikrobot MV-CT0MG0060/R/NN/NN 6 MP Area Scan Camera, 3rd Gen., GigE, Mono, BASEMV-CT0MG0060/R/NN/NN
Sensor format
1/1.8"
Lens mount
C-mount
$909.00View part

The MV-CU020-90GC covers the fast colour end: 2.5 MP, global shutter, GigE, on a 1/1.7-inch sensor behind a C-mount. The second card covers the 6 MP mono end with a 1/1.8-inch rolling-shutter GigE sensor. The choice between the two shutter types follows part motion: global for moving parts, rolling for stationary parts under strobe light.

For the host, a vision controller earns its slot as soon as camera count meets the port limit or strobe control joins the picture:

Vision controllersHikrobot MV-VC5525-256G70-NN VC5000 Vision Controller, 12th-I5, 32GB+256G SSD, Supported PCIEMV-VC5525-256G70-NN
LAN ports
5+ LAN ports
USB 3 ports
3-4 USB 3 ports
PoE
PoE camera ports
Light outputs
8 built-in channels
$4,479.00 · quotedView and quoteBuild a system with it

The MV-VC5525-256G70-NN pairs PoE camera ports and five-plus LAN ports with 32 GB of memory and eight built-in light channels. One cable per camera carries power and frames while the strobes come from the same unit.

When the usual answer is wrong: if the four views photograph one object that fits a single field of view, one higher-resolution snapshot beats four-camera plumbing. The multiple object detection in one snapshot article shows a full tray handled per cycle from one image.

Size the camera side in the camera finder or step through it in the camera configurator. If a build question survives this guide, the contact page reaches the applications team, and the vision systems topic page collects the neighbouring integration patterns.

ComputersVision controllers

Vision computers and fanless industrial PCs

190 models · from $1,119.00

Questions

Does one GigE port work if the trigger is slow?

Yes, when the arithmetic holds and the duty cycle is low. Four 6 MP mono frames serialise to about 218 ms on one 1 GbE link — acceptable for a hand-loaded station on a 500 ms cycle, tight for a continuous line. Separate ports cut the wait to about 55 ms and keep headroom for longer exposures, a faster period or a fifth camera later.

How deep should each camera's ring buffer be?

Two to three frames is a workable depth for a triggered set that processes faster than its period: one frame in transfer, one waiting, one spare. If processing can overrun a full period, raise the depth to cover the worst case and log overruns. A dropped frame surfaces later as a composite merged from two different trigger cycles, which is far harder to spot than a timeout.

How do I keep trigger skew between the four cameras small?

Wire the same PLC output to all four strobe inputs over equal cable lengths, then measure once. Input delay plus cable propagation keeps such wiring well under a millisecond of skew. Daisy-chaining one camera's output to the next adds that hop's delay per camera, so compare frame timestamps after the first composite and confirm the skew before trusting the merge.

Can I run the same pattern with USB3 cameras?

Yes — the wait-per-stream and Task.WhenAll pattern does not care about the interface; only the bandwidth numbers change. A USB3 port sustains roughly 400 MB/s, so a 12 MB frame moves in about 30 ms, and a host controller typically carries three or four such cameras without strain. Keep one wait per stream, the same composite timeout and the same per-task exception walk.

What timeout should the composite wait use?

Start at 1.5 times the trigger period — 750 ms for a 500 ms cycle. That is enough to absorb one missed frame plus transfer jitter without making the operator wait through two cycles. Log the distribution of actual composite times: a 95th percentile creeping toward the timeout means the period, the bandwidth or the processing budget is the real constraint, not the timeout value.