namespace Tizen.Applications
{
-
/// <summary>
/// Provides a synchronization context for the Tizen application model.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class TizenSynchronizationContext : SynchronizationContext
{
- private readonly Interop.Glib.GSourceFunc _wrapperHandler;
- private readonly Object _transactionLock = new Object();
- private readonly ConcurrentDictionary<int, Action> _handlerMap = new ConcurrentDictionary<int, Action>();
- private int _transactionId = 0;
-
- /// <summary>
- /// Initializes a new instance of the TizenSynchronizationContext class.
- /// </summary>
- /// <since_tizen> 3 </since_tizen>
- public TizenSynchronizationContext()
- {
- _wrapperHandler = new Interop.Glib.GSourceFunc(Handler);
- }
-
/// <summary>
/// Initilizes a new TizenSynchronizationContext and install into the current thread.
/// </summary>
SetSynchronizationContext(new TizenSynchronizationContext());
}
-
/// <summary>
/// Dispatches an asynchronous message to a Tizen main loop.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public override void Post(SendOrPostCallback d, object state)
{
- Post(() =>
+ GSourceManager.Post(() =>
{
d(state);
});
{
var mre = new ManualResetEvent(false);
Exception err = null;
- Post(() =>
+ GSourceManager.Post(() =>
{
try
{
}
}
- private void Post(Action action)
+ private static class GSourceManager
{
- int id = 0;
- lock (_transactionLock)
+ private static Interop.Glib.GSourceFunc _wrapperHandler;
+ private static Object _transactionLock;
+ private static ConcurrentDictionary<int, Action> _handlerMap;
+ private static int _transactionId;
+
+ static GSourceManager()
{
- id = _transactionId++;
+ _wrapperHandler = new Interop.Glib.GSourceFunc(Handler);
+ _transactionLock = new Object();
+ _handlerMap = new ConcurrentDictionary<int, Action>();
+ _transactionId = 0;
}
- _handlerMap.TryAdd(id, action);
- Interop.Glib.IdleAdd(_wrapperHandler, (IntPtr)id);
- }
- private bool Handler(IntPtr userData)
- {
- int key = (int)userData;
- if (_handlerMap.ContainsKey(key))
+ public static void Post(Action action)
{
- Action action;
- _handlerMap.TryRemove(key, out action);
- action?.Invoke();
+ int id = 0;
+ lock (_transactionLock)
+ {
+ id = _transactionId++;
+ }
+ _handlerMap.TryAdd(id, action);
+ Interop.Glib.IdleAdd(_wrapperHandler, (IntPtr)id);
+ }
+
+ private static bool Handler(IntPtr userData)
+ {
+ int key = (int)userData;
+ if (_handlerMap.ContainsKey(key))
+ {
+ Action action;
+ _handlerMap.TryRemove(key, out action);
+ action?.Invoke();
+ }
+ return false;
}
- return false;
}
}
}