public int RegisterDeviceEvent(EventCallbackFunction eventFunc, object obj) { unsafe { if (eventList.Count == 0) { var eventFunctionDelegate = new EventFunctionDelegate(eventFunction); if (obj != null) { eventContextHandle = GCHandle.Alloc(obj); registeredEventFunctionId = ((!_isWin) ? LinuxInterop.tdRegisterDeviceEvent(eventFunctionDelegate, (void *)GCHandle.ToIntPtr(eventContextHandle)) : WinInterop.tdRegisterDeviceEvent(eventFunctionDelegate, (void *)GCHandle.ToIntPtr(eventContextHandle))); } else { registeredEventFunctionId = ((!_isWin) ? LinuxInterop.tdRegisterDeviceEvent(eventFunctionDelegate, null) : WinInterop.tdRegisterDeviceEvent(eventFunctionDelegate, null)); } if (callbackFunctionReferenceList.ContainsKey(registeredEventFunctionId)) { callbackFunctionReferenceList.Remove(registeredEventFunctionId); } callbackFunctionReferenceList.Add(registeredEventFunctionId, eventFunctionDelegate); } this.lastEventID++; int lastEventID = this.lastEventID; eventList.Add(lastEventID, eventFunc); return(lastEventID); } }
/// <summary> /// Register a callback event function to be called when a device event (e.g. turn on, turn off) occurs /// </summary> /// <param name="eventFunc">Callback function to be called</param> /// <param name="obj">Context object that will be echoed back when function is called. Only the object when the first function is registered will be used. Set to null if not used.</param> /// <returns>Callback event id</returns> public unsafe int tdRegisterDeviceEvent(EventCallbackFunction deviceEventFunc, Object obj) { int returnValue = 0; if (deviceEventList.Count == 0) { //first added, register with dll too UnmanagedImport.EventFunctionDelegate deviceEventFunctionDelegate = new UnmanagedImport.EventFunctionDelegate(deviceEventFunction); registeredEventFunctionId = UnmanagedImport.tdRegisterDeviceEvent(deviceEventFunctionDelegate, (void *)null); //context here or above? GC.Collect(); callbackFunctionReferenceList.Add(registeredEventFunctionId, deviceEventFunctionDelegate); } ++lastEventID; returnValue = lastEventID; DeviceEventFunctionContext deviceEventFuncContext = new DeviceEventFunctionContext(); deviceEventFuncContext.eventCallbackFunc = deviceEventFunc; deviceEventFuncContext.context = obj; deviceEventFuncContext.callbackId = returnValue; deviceEventList.Add(returnValue, deviceEventFuncContext); return(returnValue); }
public int RegisterDeviceEvent(EventCallbackFunction eventFunc, object obj) { unsafe { if (eventList.Count == 0) { var eventFunctionDelegate = new EventFunctionDelegate(eventFunction); if (obj != null) { eventContextHandle = GCHandle.Alloc(obj); registeredEventFunctionId = ((!_isWin) ? LinuxInterop.tdRegisterDeviceEvent(eventFunctionDelegate, (void*) GCHandle.ToIntPtr(eventContextHandle)) : WinInterop.tdRegisterDeviceEvent(eventFunctionDelegate, (void*) GCHandle.ToIntPtr(eventContextHandle))); } else { registeredEventFunctionId = ((!_isWin) ? LinuxInterop.tdRegisterDeviceEvent(eventFunctionDelegate, null) : WinInterop.tdRegisterDeviceEvent(eventFunctionDelegate, null)); } if (callbackFunctionReferenceList.ContainsKey(registeredEventFunctionId)) { callbackFunctionReferenceList.Remove(registeredEventFunctionId); } callbackFunctionReferenceList.Add(registeredEventFunctionId, eventFunctionDelegate); } this.lastEventID++; int lastEventID = this.lastEventID; eventList.Add(lastEventID, eventFunc); return lastEventID; } }
/// <summary> /// Register a callback event function to be called when a device event (e.g. turn on, turn off) occurs /// </summary> /// <param name="eventFunc">Callback function to be called</param> /// <param name="obj">Context object that will be echoed back when function is called. Only the object when the first function is registered will be used. Set to null if not used.</param> /// <returns>Callback event id</returns> public unsafe int tdRegisterDeviceEvent(EventCallbackFunction deviceEventFunc, Object obj) { int returnValue = 0; if (deviceEventList.Count == 0) { //first added, register with dll too UnmanagedImport.EventFunctionDelegate deviceEventFunctionDelegate = new UnmanagedImport.EventFunctionDelegate(deviceEventFunction); registeredEventFunctionId = UnmanagedImport.tdRegisterDeviceEvent(deviceEventFunctionDelegate, (void*)null); //context here or above? GC.Collect(); callbackFunctionReferenceList.Add(registeredEventFunctionId, deviceEventFunctionDelegate); } ++lastEventID; returnValue = lastEventID; DeviceEventFunctionContext deviceEventFuncContext = new DeviceEventFunctionContext(); deviceEventFuncContext.eventCallbackFunc = deviceEventFunc; deviceEventFuncContext.context = obj; deviceEventFuncContext.callbackId = returnValue; deviceEventList.Add(returnValue, deviceEventFuncContext); return returnValue; }