Class GenICamCameraManager
- Namespace
- VisioForge.Core.GenICam
- Assembly
- VisioForge.Core.dll
Static class for centralized camera management and enumeration of GenICam-compatible cameras.
This class provides centralized management and enumeration of GenICamCamera instances to ensure:
- Each camera is created only once and reused across the application
- Simple open/closed state tracking with thread-safe operations
- Thread-safe access to camera instances using concurrent collections
- Automatic cleanup when cameras are no longer needed or available
- Comprehensive device enumeration and discovery with real-time updates
- Proper resource management and disposal patterns
Thread Safety: This class is fully thread-safe and can be used from multiple threads simultaneously.
Usage Pattern:
- Call VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList to populate the camera dictionary with available devices
- Use VisioForge.Core.GenICam.GenICamCameraManager.GetCamera(System.String) to get a camera reference by device ID
- Call VisioForge.Core.GenICam.GenICamCameraManager.OpenCamera(System.String) before using the camera for operations
- Perform camera operations (streaming, configuration, etc.)
- Call VisioForge.Core.GenICam.GenICamCameraManager.CloseCamera(System.String) when done with the camera
Resource Management:
Cameras are automatically cleaned up when they become unavailable. The manager tracks device availability and removes cameras that are no longer present in the system during VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList calls.
// Enumerate and populate camera dictionary
GenICamCameraManager.UpdateDeviceList();
// Get camera reference
var camera = GenICamCameraManager.GetCamera("MyCamera");
if (camera == null)
{
Console.WriteLine("Camera not found");
return;
}
// Open camera for use
if (GenICamCameraManager.OpenCamera("MyCamera"))
{
try
{
// Use camera for operations
camera.StartStreaming();
// Perform image capture operations
// ... do work ...
camera.StopStreaming();
}
finally
{
// Always close camera when done
GenICamCameraManager.CloseCamera("MyCamera");
}
}
else
{
Console.WriteLine("Failed to open camera");
}</code></pre></example>
public static class GenICamCameraManager
Inheritance
-
Inherited Members
-
Methods
CloseCamera(string)
Closes a camera and disconnects it from the hardware without removing it from the cache.
public static bool CloseCamera(string deviceId)
Parameters
deviceId string
-
The unique device ID of the camera to close.
Returns
- bool
-
true if the camera was successfully closed or was already closed;
false if an error occurred during the closing process.
Examples
string deviceId = "MyCamera_12345";
// Open camera
if (GenICamCameraManager.OpenCamera(deviceId))
{
try
{
// Use camera...
var camera = GenICamCameraManager.GetCamera(deviceId);
camera.StartStreaming();
// ... perform operations ...
}
finally
{
// Always close when done
bool closed = GenICamCameraManager.CloseCamera(deviceId);
if (!closed)
{
Console.WriteLine("Warning: Camera may not have closed properly");
}
}
}
Remarks
This method performs the following operations:
- Stops any active streaming on the camera
- Disconnects the camera from the hardware
- Keeps the camera instance in cache for potential reuse
Thread Safety: This method is thread-safe and uses internal locking.
Resource Management: This method properly releases hardware resources
while maintaining the camera instance for future use.
Idempotent: It's safe to call this method multiple times on the same camera.
FindCameraBySerialNumber(string)
Finds a camera by its exact serial number using case-insensitive matching.
public static GenICamCamera FindCameraBySerialNumber(string serialNumber)
Parameters
serialNumber string
-
The serial number to search for.
Returns
- GenICamCamera
-
A VisioForge.Core.GenICam.GenICamCamera instance with the matching serial number,
or null if no camera with that serial number is found.
Examples
// Find camera by serial number
var camera = GenICamCameraManager.FindCameraBySerialNumber("12345678");
if (camera != null)
{
Console.WriteLine($"Found camera: {camera.DeviceId}");
// Open and use the camera
if (GenICamCameraManager.OpenCamera(camera.DeviceId))
{
// Use camera...
GenICamCameraManager.CloseCamera(camera.DeviceId);
}
}
else
{
Console.WriteLine("Camera with serial number 12345678 not found");
}
Remarks
This method performs a case-insensitive exact match search for cameras
with the specified serial number.
Performance: This method may take some time as it needs to query
device information for each available camera until a match is found.
Cameras that cannot be queried are skipped with a warning logged.
Uniqueness: Serial numbers should be unique, so this method
returns the first match found.
FindCamerasByModel(string)
Finds cameras by model name using case-insensitive partial matching.
public static List<GenICamCamera> FindCamerasByModel(string modelName)
Parameters
modelName string
-
The model name or partial model name to search for.
Returns
- List<GenICamCamera>
-
A list of VisioForge.Core.GenICam.GenICamCamera instances with models matching the specified name.
Returns an empty list if no matches are found.
Examples
// Find all cameras with model names containing "ace"
var aceCameras = GenICamCameraManager.FindCamerasByModel("ace");
Console.WriteLine($"Found {aceCameras.Count} cameras with 'ace' in model name");
foreach (var camera in aceCameras)
{
Console.WriteLine($" - {camera.DeviceId}");
}
Remarks
This method performs a case-insensitive search for cameras whose model name
contains the specified string. Partial matches are supported.
Performance: This method may take some time as it needs to query
device information for each available camera.
Cameras that cannot be queried are skipped with a warning logged.
FindCamerasByVendor(string)
Finds cameras by vendor name using case-insensitive partial matching.
public static List<GenICamCamera> FindCamerasByVendor(string vendorName)
Parameters
vendorName string
-
The vendor name or partial vendor name to search for.
Returns
- List<GenICamCamera>
-
A list of VisioForge.Core.GenICam.GenICamCamera instances from vendors matching the specified name.
Returns an empty list if no matches are found.
Examples
// Find all cameras from vendors containing "Basler"
var baslerCameras = GenICamCameraManager.FindCamerasByVendor("Basler");
Console.WriteLine($"Found {baslerCameras.Count} Basler cameras");
foreach (var camera in baslerCameras)
{
Console.WriteLine($" - {camera.DeviceId}");
}
Remarks
This method performs a case-insensitive search for cameras whose vendor name
contains the specified string. Partial matches are supported.
Performance: This method may take some time as it needs to query
device information for each available camera.
Cameras that cannot be queried are skipped with a warning logged.
ForceCleanup()
Forces removal and disposal of all cameras from the cache, regardless of their current state.
public static void ForceCleanup()
Examples
// Application shutdown sequence
try
{
// Attempt graceful shutdown first
foreach (var deviceId in GetDeviceIds())
{
CloseCamera(deviceId);
}
}
catch (Exception ex)
{
Console.WriteLine($"Graceful shutdown failed: {ex.Message}");
}
finally
{
// Force cleanup as last resort
GenICamCameraManager.ForceCleanup();
}
Remarks
⚠️ WARNING: This method should be used with extreme caution as it will dispose
all camera instances even if they are currently in use. This can cause exceptions in other parts
of the application that are actively using cameras.
This method is intended for use in the following scenarios:
- Application shutdown
- Recovery from unrecoverable camera states
- Memory cleanup in testing scenarios
Thread Safety: This method is thread-safe but will affect all threads using cameras.
Recovery: After calling this method, you must call VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList
to repopulate the camera cache before using cameras again.
GetCamera(string)
Gets a camera instance by device ID, creating it if necessary but not connecting to it.
public static GenICamCamera GetCamera(string deviceId)
Parameters
deviceId string
-
The unique device ID of the camera to retrieve.
Returns
- GenICamCamera
-
A VisioForge.Core.GenICam.GenICamCamera instance for the specified device, or null if the device ID is invalid or the camera cannot be created.
Examples
// First, update the device list
GenICamCameraManager.UpdateDeviceList();
// Get a camera instance
var camera = GenICamCameraManager.GetCamera("MyCamera_12345");
if (camera != null)
{
Console.WriteLine($"Camera retrieved: {camera.DeviceId}");
}
else
{
Console.WriteLine("Camera not found or could not be created");
}
Remarks
This method returns a camera instance from the internal cache. If the camera doesn't exist in the cache,
it will attempt to create a new instance. The camera is not automatically connected.
Important: You must call VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList before using this method
to ensure the device is available in the enumerated list.
Use VisioForge.Core.GenICam.GenICamCameraManager.OpenCamera(System.String) to connect to the camera before performing operations.
GetCamera(uint)
Gets a camera instance by its enumeration index.
public static GenICamCamera GetCamera(uint index)
Parameters
index uint
-
The zero-based index of the camera in the device enumeration.
Returns
- GenICamCamera
-
A VisioForge.Core.GenICam.GenICamCamera instance for the camera at the specified index.
Examples
try
{
var firstCamera = GenICamCameraManager.GetCamera(0);
Console.WriteLine($"First camera: {firstCamera.DeviceId}");
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine("No cameras available");
}
catch (AravisException ex)
{
Console.WriteLine($"Error getting camera: {ex.Message}");
}
Remarks
This method is a convenience wrapper that gets the device ID for the specified index
and then retrieves the corresponding camera instance.
Note: Device indices may change between enumerations. For consistent
access, consider using device IDs instead of indices.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the specified index is greater than or equal to the number of available devices.
- AravisException
-
Thrown when the camera cannot be retrieved or created for the specified index.
GetCameras()
Gets all available cameras as VisioForge.Core.GenICam.GenICamCamera instances from the centralized cache.
public static List<GenICamCamera> GetCameras()
Returns
- List<GenICamCamera>
-
A list of VisioForge.Core.GenICam.GenICamCamera instances for all available cameras.
Cameras that cannot be retrieved are excluded from the list.
Examples
// Get all available cameras
var cameras = GenICamCameraManager.GetCameras();
Console.WriteLine($"Found {cameras.Count} cameras:");
foreach (var camera in cameras)
{
Console.WriteLine($" - {camera.DeviceId}");
}
Remarks
This method retrieves camera instances for all devices found in the most recent enumeration.
Cameras that cannot be created or retrieved are skipped with a warning logged.
The returned cameras are not automatically connected. Use VisioForge.Core.GenICam.GenICamCameraManager.OpenCamera(System.String)
to connect to individual cameras before use.
Performance: This method may take some time if there are many cameras
or if some cameras are slow to respond during instantiation.
GetCamerasWithInfo()
Gets all cameras with their detailed information loaded by temporarily opening each camera.
public static List<GenICamCamera> GetCamerasWithInfo()
Returns
- List<GenICamCamera>
-
A list of VisioForge.Core.GenICam.GenICamCamera instances with their VisioForge.Core.GenICam.GenICamCamera properties
populated with detailed camera information.
Examples
// Get all cameras with detailed information
var camerasWithInfo = GenICamCameraManager.GetCamerasWithInfo();
Console.WriteLine($"Found {camerasWithInfo.Count} cameras with detailed info:");
foreach (var camera in camerasWithInfo)
{
var info = camera.CameraInfo;
Console.WriteLine($" - {info.DeviceId}");
Console.WriteLine($" Vendor: {info.Vendor}");
Console.WriteLine($" Model: {info.Model}");
Console.WriteLine($" Serial: {info.SerialNumber}");
Console.WriteLine($" Resolution: {info.Width}x{info.Height}");
}
Remarks
This method temporarily opens each camera to read its detailed information,
then closes it immediately. This provides comprehensive camera information but
has the following implications:
-
Performance: This operation can be slow as it connects to each camera
-
Exclusive Access: Cameras in use by other applications will be skipped
-
Resource Usage: Temporarily uses camera resources for each device
Use Cases:
- Camera discovery and enumeration interfaces
- Detailed camera capability reporting
- Camera selection based on specifications
Cameras that cannot be opened or provide information are excluded from the results.
GetDeviceCount()
Gets the number of currently available GenICam-compatible devices.
public static uint GetDeviceCount()
Returns
- uint
-
The number of available cameras detected by the system.
Examples
GenICamCameraManager.UpdateDeviceList();
uint count = GenICamCameraManager.GetDeviceCount();
Console.WriteLine($"Available cameras: {count}");
Remarks
This method returns the count from the most recent device enumeration.
Call VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList to refresh the device list before calling this method
if you need the most up-to-date count.
GetDeviceId(uint)
Gets the device ID for a specific camera by its enumeration index.
public static string GetDeviceId(uint index)
Parameters
index uint
-
The zero-based index of the device in the enumeration list.
Returns
- string
-
The device ID string for the specified camera, or "Unknown" if the ID cannot be determined.
Examples
try
{
string firstDeviceId = GenICamCameraManager.GetDeviceId(0);
Console.WriteLine($"First camera ID: {firstDeviceId}");
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine("No cameras available");
}
Remarks
This method automatically calls VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList to ensure the most current device list.
Device indices are assigned in the order devices are discovered and may change between enumerations.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the specified index is greater than or equal to the number of available devices.
GetDeviceIds()
Gets device IDs for all currently available GenICam-compatible cameras.
public static string[] GetDeviceIds()
Returns
- string[]
-
An array of device ID strings. Each string uniquely identifies a camera device.
Examples
string[] deviceIds = GenICamCameraManager.GetDeviceIds();
foreach (string deviceId in deviceIds)
{
Console.WriteLine($"Found camera: {deviceId}");
}
Remarks
This method automatically calls VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList to ensure the most current device list.
Device IDs are typically in the format "Vendor_Model_SerialNumber" but the exact format depends on the camera manufacturer.
The returned array will contain "Unknown" for devices where the ID cannot be determined.
GetFirstAvailableCamera()
Gets the first available camera from the centralized cache.
public static GenICamCamera GetFirstAvailableCamera()
Returns
- GenICamCamera
-
A VisioForge.Core.GenICam.GenICamCamera instance for the first available camera.
Examples
try
{
var camera = GenICamCameraManager.GetFirstAvailableCamera();
Console.WriteLine($"Using camera: {camera.DeviceId}");
// Open and use the camera
if (GenICamCameraManager.OpenCamera(camera.DeviceId))
{
// Use camera...
GenICamCameraManager.CloseCamera(camera.DeviceId);
}
}
catch (AravisException ex)
{
Console.WriteLine($"No cameras available: {ex.Message}");
}
Remarks
This is a convenience method for applications that only need to work with a single camera
or want to use the first available camera by default.
This method automatically calls VisioForge.Core.GenICam.GenICamCameraManager.UpdateDeviceList to ensure current device information.
The returned camera is not automatically connected. Use VisioForge.Core.GenICam.GenICamCameraManager.OpenCamera(System.String)
to connect to the camera before use.
Exceptions
- AravisException
-
Thrown when no cameras are available or the first camera cannot be retrieved.
IsCameraOpen(string)
Checks if a camera is currently open and connected to the hardware.
public static bool IsCameraOpen(string deviceId)
Parameters
deviceId string
-
The unique device ID of the camera to check.
Returns
- bool
-
true if the camera is currently open and connected;
false if the camera is closed, not found, or the device ID is invalid.
Examples
string deviceId = "MyCamera_12345";
if (GenICamCameraManager.IsCameraOpen(deviceId))
{
Console.WriteLine("Camera is ready for use");
var camera = GenICamCameraManager.GetCamera(deviceId);
// Use camera...
}
else
{
Console.WriteLine("Camera needs to be opened first");
GenICamCameraManager.OpenCamera(deviceId);
}
Remarks
This method provides a quick way to check camera connection status without
attempting to open or modify the camera state.
Thread Safety: This method is thread-safe and can be called from any thread.
Performance: This is a lightweight operation that only checks internal state.
OpenCamera(string)
Opens a camera for use by establishing a connection to the hardware.
public static bool OpenCamera(string deviceId)
Parameters
deviceId string
-
The unique device ID of the camera to open.
Returns
- bool
-
true if the camera was successfully opened and is ready for use;
false if the camera could not be opened.
Examples
string deviceId = "MyCamera_12345";
if (GenICamCameraManager.OpenCamera(deviceId))
{
try
{
var camera = GenICamCameraManager.GetCamera(deviceId);
// Use camera for operations
camera.StartStreaming();
// ... perform operations ...
camera.StopStreaming();
}
finally
{
GenICamCameraManager.CloseCamera(deviceId);
}
}
else
{
Console.WriteLine("Failed to open camera");
}
Remarks
This method performs the following operations:
- Validates the device ID and retrieves the camera instance
- Checks if the camera is already connected (returns true if so)
- Establishes a hardware connection to the camera
- Reads camera information to populate detailed properties
Thread Safety: This method is thread-safe and uses internal locking.
Resource Management: Always call VisioForge.Core.GenICam.GenICamCameraManager.CloseCamera(System.String) when done
with the camera to properly release resources.
Error Handling: Detailed error information is logged. Check logs for specific failure reasons.
SetContext(BaseContext)
Sets the BaseContext for logging operations throughout the camera manager.
public static void SetContext(BaseContext context)
Parameters
context BaseContext
-
BaseContext instance for logging. If null, a new default context will be created.
Examples
var context = new BaseContext();
GenICamCameraManager.SetContext(context);
Remarks
This method should be called early in the application lifecycle to ensure
proper logging throughout camera operations.
UpdateDeviceList()
Updates the device list by scanning for available GenICam-compatible cameras.
Automatically cleans up cameras that are no longer available in the system.
public static void UpdateDeviceList()
Examples
// Refresh the list of available cameras
GenICamCameraManager.UpdateDeviceList();
// Get count of available devices
uint deviceCount = GenICamCameraManager.GetDeviceCount();
Console.WriteLine($"Found {deviceCount} cameras");
Remarks
This method performs the following operations:
- Calls the underlying Aravis system to refresh the device list
- Compares current devices with cached camera instances
- Removes cameras that are no longer available (if not connected)
- Adds new cameras to the internal dictionary
Thread Safety: This method is thread-safe and can be called from multiple threads.
Performance: This method may take some time to complete as it performs hardware enumeration.
Consider calling it on a background thread in UI applications.