ObjectDetectionClient
namespace AR51.Net;
public sealed class ObjectDetectionClient : IObjectDetectionClient, IAsyncDisposable
Implements: IObjectDetectionClient · IAsyncDisposable
Dispose it with await using (or DisposeAsync): this stops every stream and closes every connection. Calling it again does nothing.
Receives live tracked objects from the AR 51 object detection service. Get one from IAR51Client.ObjectDetection (recommended: the server is found for you), or create one on its own with the service's address.
This page is reference (facts only). For a walkthrough with a complete sample, see the Object detection guide.
await using var objects = new ObjectDetectionClient("192.168.1.20", 1503);
objects.ObjectReceived += (_, e) => Console.WriteLine($"{e.Object.Name} at {e.Object.Position}");
await objects.StartAsync();
Properties
| Property | Type | Access | Description |
|---|---|---|---|
IsAvailable | bool | get | true when the object detection service's address is known (always, for a standalone ObjectDetectionClient). Implements IObjectDetectionClient.IsAvailable. |
Method summary
| Method | Returns | |
|---|---|---|
Constructor | — | constructor |
StartAsync | Task | implements |
StopAsync | Task | implements |
StreamAsync | IAsyncEnumerable<TrackedObject> | implements |
DisposeAsync | ValueTask |
→ Full descriptions in Method details below.
Events
Standard .NET events (EventHandler<T>): subscribe with +=, unsubscribe with -=. They are raised on a background thread.
| Event | Args | Raised when |
|---|---|---|
ObjectReceived | TrackedObjectEventArgs | Raised for every object received while started, one event per object per frame, on a background thread. Keep handlers short: they run on the receiving thread. An exception thrown by a handler is logged and does not stop the others or the stream. Implements IObjectDetectionClient.ObjectReceived. |
Method details
Constructor
public ObjectDetectionClient(string host, int port, ILoggerFactory? loggerFactory = null);
Creates a standalone client for the object detection service at host:port. Call StartAsync to begin receiving.
host is empty.port is not between 1 and 65535.AR 51 service ports are not fixed. Prefer IAR51Client.ObjectDetection, which finds the service for you.
StartAsync
public Task StartAsync(CancellationToken cancellationToken = default);
Starts receiving objects into ObjectReceived. Returns at once; calling it again does nothing.
Receiving begins as soon as the service is reachable, and resumes by itself after the service restarts or the network drops, until StopAsync or dispose.
StopAsync
public Task StopAsync(CancellationToken cancellationToken = default);
Stops receiving objects: stops ObjectReceived, ends every StreamAsync loop running now, and waits until the event stream is closed. Calling it again does nothing.
StreamAsync loops end normally, without an exception, after the object they are handling. Loops started after this call are not affected.
StreamAsync
public IAsyncEnumerable<TrackedObject> StreamAsync([EnumeratorCancellation] CancellationToken cancellationToken = default);
Streams objects until StopAsync is called (the loop ends normally), cancellationToken is cancelled, or the connection drops. Works without StartAsync.
cancellationToken was cancelled.Each call opens its own stream, which closes when the loop ends. Unlike ObjectReceived, it does not reconnect: start a new loop after an AR51Exception.
DisposeAsync
public ValueTask DisposeAsync();
Stops receiving, closes every stream and, for a standalone client, its connection. Calling it again does nothing.
See also
- Object detection guide — walkthrough with a complete, compiled sample
- Class index — every AR51.Net type
- Data conventions, threading and errors