[DllImport(Libraries.Glib, EntryPoint = "g_idle_add", CallingConvention = CallingConvention.Cdecl)]
internal static extern uint IdleAdd(GSourceFunc d, IntPtr data);
+
+ [DllImport(Libraries.Glib, EntryPoint = "g_source_remove", CallingConvention = CallingConvention.Cdecl)]
+ internal static extern bool RemoveSource(uint source);
}
}
using System;
using System.Collections.Concurrent;
+using System.Collections.Generic;
using System.Threading;
namespace Tizen.Applications
{
private readonly Interop.Glib.GSourceFunc _wrapperHandler;
private readonly Object _transactionLock = new Object();
- private readonly ConcurrentDictionary<int, Action> _handlerMap = new ConcurrentDictionary<int, Action>();
+ private readonly ConcurrentDictionary<int, KeyValuePair<Action,uint>> _handlerMap
+ = new ConcurrentDictionary<int, KeyValuePair<Action, uint>>();
private int _transactionId = 0;
/// <summary>
}
/// <summary>
+ /// Finalizer to clean up the native source
+ /// </summary>
+ ~TizenSynchronizationContext()
+ {
+ foreach (var s in _handlerMap.Values)
+ {
+ if (s.Value > 0)
+ Interop.Glib.RemoveSource(s.Value);
+ }
+ }
+
+ /// <summary>
/// Initilizes a new TizenSynchronizationContext and install into the current thread.
/// </summary>
/// <remarks>
{
id = _transactionId++;
}
- _handlerMap.TryAdd(id, action);
- Interop.Glib.IdleAdd(_wrapperHandler, (IntPtr)id);
+ _handlerMap.TryAdd(id, new KeyValuePair<Action,uint>(action,
+ Interop.Glib.IdleAdd(_wrapperHandler, (IntPtr)id)));
}
private bool Handler(IntPtr userData)
int key = (int)userData;
if (_handlerMap.ContainsKey(key))
{
- Action action;
- _handlerMap.TryRemove(key, out action);
- action?.Invoke();
+ _handlerMap.TryRemove(key, out var p);
+ p.Key?.Invoke();
}
return false;
}