Exception thrown during deconnection with D435
Hi All,
I have tried to develop a C# class for multiple IntelRealSense D435 cameras.
I tried to use a delegate function on OnDeviceChanged event, that should arise with connection/deconnection of camera. The problem is when I disconnect the camera a CallbackOnCollectedDelegate exception is thrown.
public class DepthCameraCluster<T> where T : IDisposable
{
#region Fields
private static Dictionary<string, DepthCamera<T>> depthCameras;
private static Dictionary<string, ProductInfo> availableCameras;
private static Context context;
private static bool stopThread;
private static Task AcquisitionTask;
/// <summary>
/// The delegate function declares as field to avoid to be garbage collected
/// </summary>
public static Context.OnDevicesChangedDelegate handleDeconnection;
#endregion
#region Constructors
static DepthCameraCluster()
{
depthCameras = new Dictionary<string, DepthCamera<T>>();
stopThread = false;
AcquisitionTask = null;
UpdateCameraCluster();
}
[...]
private static void UpdateCameraCluster()
{
Context = new Context();
availableCameras = new Dictionary<string, ProductInfo>();
using (var devices = Context.QueryDevices())
{
foreach (var device in devices)
{
ProductInfo prodInfo = new ProductInfo(device.Info[CameraInfo.FirmwareVersion], device.Info[CameraInfo.Name], device.Info[CameraInfo.UsbTypeDescriptor]);
availableCameras.Add(device.Info[CameraInfo.SerialNumber], prodInfo);
}
foreach (var pair in depthCameras)
{
if (!availableCameras.ContainsKey(pair.Key))
{
//Stop the processing thread of the camera
StopVideo(pair.Key);
Console.WriteLine("Depth Camera with Serial Number " + pair.Key + " lost connection.");
}
else
StartCamera(pair.Key);
}
}
handleDeconnection = new Context.OnDevicesChangedDelegate(HandlerOnDevicesChanged);
if (handleDeconnection == null) Console.WriteLine("Handle deconnection null");
Context.OnDevicesChanged += handleDeconnection;
//handleDeconnection?.Invoke(null, null);
}
/// <summary>
/// Function called when a new connection is found or delete
/// </summary>
/// <param name="removed_devices"></param>
/// <param name="added_devices"></param>
private static void HandlerOnDevicesChanged(DeviceList removed_devices, DeviceList added_devices)
{
string removed_devices_result = "Removed: " + removed_devices.Count.ToString() + Environment.NewLine;
if (removed_devices.Count > 0)
{
for (int i = 0; i < removed_devices.Count; i++)
{
string device_info;
try
{
device_info = removed_devices[i].Info.GetInfo(CameraInfo.Name) + " " + removed_devices[i].Info.GetInfo(CameraInfo.SerialNumber);
}
catch
{
device_info = "Cannot get info for device #" + (i + 1).ToString();
}
removed_devices_result = removed_devices_result + device_info + Environment.NewLine;
}
}
string added_devices_result = "Added: ";
added_devices_result = added_devices_result + added_devices.Count.ToString() + Environment.NewLine;
if (added_devices.Count > 0)
{
for (int i = 0; i < added_devices.Count; i++)
{
string device_info;
try
{
device_info = added_devices[i].Info.GetInfo(CameraInfo.Name) + " " + added_devices[i].Info.GetInfo(CameraInfo.SerialNumber);
}
catch
{
device_info = "Cannot get info for device #" + (i + 1).ToString();
}
added_devices_result = added_devices_result + device_info + Environment.NewLine;
}
}
UpdateCameraCluster();
}
The method where I register the delegate function is UpdateCameraCluster which is called during initialization of the class and normally each time a OnDeviceChanged event arise.
After some researches, I saw that to avoid this exception the delegate function should be declared as global. Unfortunately, this action didn't solve my problem any idea/suggestion to solve this problem.
Thank you for your help
Hugo
-
Hi Hcharrier Scripting for detection of connection and disconnection events in C# is discussed in the link below. I hope that the resources suggested in the discussion will be helpful to you.
-
Hi MartyG, thank you for your help.
I already saw this topic but I was hopping I could use the event as it is dedicated for that scenario... Anyway, I finally check for lost/found connection in a thread as it was suggest in the link.
Best regards
Hugo
-
Thanks very much Hcharrier for the update. Were you successful with checking in a thread or do you require further assistance, please?
-
MartyG yes I succeed to make it work, now it is detecting lost connection and tries to relaunch the stream once the connection is re-established.
Please sign in to leave a comment.
Comments
5 comments