public void CleanUp(bool closeAll) { try { if (registeredEventFunctionId != -1) { if (_isWin) { WinInterop.tdUnregisterCallback(registeredEventFunctionId); } else { LinuxInterop.tdUnregisterCallback(registeredEventFunctionId); } registeredEventFunctionId = -1; } if (registeredDeviceChangeEventFunctionId != -1) { if (_isWin) { WinInterop.tdUnregisterCallback(registeredDeviceChangeEventFunctionId); } else { LinuxInterop.tdUnregisterCallback(registeredDeviceChangeEventFunctionId); } registeredDeviceChangeEventFunctionId = -1; } if (registeredRawListenerFunctionId != -1) { if (_isWin) { WinInterop.tdUnregisterCallback(registeredRawListenerFunctionId); } else { LinuxInterop.tdUnregisterCallback(registeredRawListenerFunctionId); } registeredRawListenerFunctionId = -1; } if (registeredSensorListenerFunctionId != -1) { if (_isWin) { WinInterop.tdUnregisterCallback(registeredSensorListenerFunctionId); } else { LinuxInterop.tdUnregisterCallback(registeredSensorListenerFunctionId); } registeredSensorListenerFunctionId = -1; } eventList.Clear(); deviceChangeEventList.Clear(); rawListenerList.Clear(); sensorListenerList.Clear(); if (closeAll) { Close(); } if (eventContextHandle.IsAllocated) { eventContextHandle.Free(); } if (deviceChangeEventContextHandle.IsAllocated) { deviceChangeEventContextHandle.Free(); } if (rawListenerContextHandle.IsAllocated) { rawListenerContextHandle.Free(); } if (sensorListenerContextHandle.IsAllocated) { sensorListenerContextHandle.Free(); } } catch (Exception e) { Console.WriteLine("Error during cleanup:" + e.ToString()); } }