_replyMap[taskID] = (int result, IntPtr data) =>
{
- _replyMap.Remove((int)data);
try
{
CheckError((SessionError)result, "Interop failed to complete adding a new subsession user");
+ task.SetResult(true);
}
catch (Exception exception)
{
task.SetException(exception);
- return;
}
-
- task.SetResult(true);
+ finally
+ {
+ _replyMap.Remove((int)data);
+ }
};
SessionError ret = Interop.Session.SubsessionAddUser(SessionUID, userName, _replyMap[taskID], (IntPtr)taskID);
catch (Exception exception)
{
task.SetException(exception);
- return;
}
finally
{
catch (Exception exception)
{
task.SetException(exception);
- return;
}
finally
{
lock (_addUserWaitLock)
{
if (_addUserWaitHandler == null)
- RegisterCallbackForEvent(SessionEventType.AddUserWait, _addUserWaitCB, OnAddUserWait);
+ RegisterCallbackForEvent(SessionEventType.AddUserWait, ref _addUserWaitCB, OnAddUserWait);
_addUserWaitHandler += value;
}
}
{
_addUserWaitHandler -= value;
if (_addUserWaitHandler == null)
- UnregisterCallbackForEvent(SessionEventType.AddUserWait, _addUserWaitCB);
+ UnregisterCallbackForEvent(SessionEventType.AddUserWait, ref _addUserWaitCB);
}
}
}
lock (_removeUserWaitLock)
{
if (_removeUserWaitHandler == null)
- RegisterCallbackForEvent(SessionEventType.RemoveUserWait, _removeUserWaitCB, OnRemoveUserWait);
+ RegisterCallbackForEvent(SessionEventType.RemoveUserWait, ref _removeUserWaitCB, OnRemoveUserWait);
_removeUserWaitHandler += value;
}
}
{
_removeUserWaitHandler -= value;
if (_removeUserWaitHandler == null)
- UnregisterCallbackForEvent(SessionEventType.RemoveUserWait, _removeUserWaitCB);
+ UnregisterCallbackForEvent(SessionEventType.RemoveUserWait, ref _removeUserWaitCB);
}
}
}
lock (_switchUserWaitLock)
{
if (_switchUserWaitHandler == null)
- RegisterCallbackForEvent(SessionEventType.SwitchUserWait, _switchUserWaitCB, OnSwitchUserWait);
+ RegisterCallbackForEvent(SessionEventType.SwitchUserWait, ref _switchUserWaitCB, OnSwitchUserWait);
_switchUserWaitHandler += value;
}
}
{
_switchUserWaitHandler -= value;
if (_switchUserWaitHandler == null)
- UnregisterCallbackForEvent(SessionEventType.SwitchUserWait, _switchUserWaitCB);
+ UnregisterCallbackForEvent(SessionEventType.SwitchUserWait, ref _switchUserWaitCB);
}
}
}
lock (_switchUserCompletionLock)
{
if (_switchUserCompletionHandler == null)
- RegisterCallbackForEvent(SessionEventType.SwitchUserCompletion, _switchUserCompletionCB, OnSwitchUserCompletion);
+ RegisterCallbackForEvent(SessionEventType.SwitchUserCompletion, ref _switchUserCompletionCB, OnSwitchUserCompletion);
_switchUserCompletionHandler += value;
}
}
{
_switchUserCompletionHandler -= value;
if (_switchUserCompletionHandler == null)
- UnregisterCallbackForEvent(SessionEventType.SwitchUserCompletion, _switchUserCompletionCB);
+ UnregisterCallbackForEvent(SessionEventType.SwitchUserCompletion, ref _switchUserCompletionCB);
}
}
}
throw ex;
}
- private void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
+ static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
{
- managedArray = new string[size];
-
- byte[] byteArray = new byte[MaxUserLength * size];
- byte[] tmpArray = new byte[MaxUserLength];
-
- Marshal.Copy(unmanagedArray, byteArray, 0, MaxUserLength * size);
-
- for (int i = 0; i < size; i++)
+ managedArray = new string[size];
+ var curr = unmanagedArray;
+
+ for (int iterator = 0; iterator < size; iterator++)
{
- Array.Copy(byteArray, MaxUserLength * i, tmpArray, 0, MaxUserLength);
- managedArray[i] = Encoding.UTF8.GetString(tmpArray);
+ managedArray[iterator] = Marshal.PtrToStringAnsi(curr, 20);
+ curr = IntPtr.Add(curr, 20);
}
}
- private void RegisterCallbackForEvent(SessionEventType eventType, Interop.Session.SubsessionEventCallback eventCallback,
+ private void RegisterCallbackForEvent(SessionEventType eventType, ref Interop.Session.SubsessionEventCallback eventCallback,
EventDelegate delegateToSet)
{
eventCallback = new Interop.Session.SubsessionEventCallback(delegateToSet);
CheckError(ret, $"Interop failed to register a callback for an event of type {eventType}");
}
- private void UnregisterCallbackForEvent(SessionEventType eventType, Interop.Session.SubsessionEventCallback eventCallback)
+ private void UnregisterCallbackForEvent(SessionEventType eventType, ref Interop.Session.SubsessionEventCallback eventCallback)
{
SessionError ret = Interop.Session.SubesssionUnregisterEventCallback(SessionUID, eventType);
CheckError(ret, $"Interop failed to unregister a callback for an event of type {eventType}");