2 using System.Collections.Generic;
6 public static class EcoreMainloop
8 static readonly Dictionary<int, Func<bool>> _taskMap = new Dictionary<int, Func<bool>>();
9 static readonly Object _taskLock = new Object();
10 static int _newTaskId = 0;
13 static Interop.Ecore.EcoreTaskCallback _nativeHandler;
15 static EcoreMainloop()
17 Interop.Ecore.ecore_init();
18 Interop.Ecore.ecore_main_loop_glib_integrate();
19 _nativeHandler = NativeHandler;
22 public static void Begin()
24 Interop.Ecore.ecore_main_loop_begin();
27 public static void Quit()
29 Interop.Ecore.ecore_main_loop_quit();
32 public static void Post(Action task)
34 int id = RegistHandler(() => { task(); return false; });
35 Interop.Ecore.ecore_idler_add(_nativeHandler, (IntPtr)id);
38 public static void PostAndWakeUp(Action task)
40 int id = RegistHandler(() => { task(); return false; });
41 Interop.Ecore.ecore_main_loop_thread_safe_call_async(_nativeHandler, (IntPtr)id);
44 public static void Send(Action task)
46 int id = RegistHandler(() => { task(); return false; });
47 Interop.Ecore.ecore_main_loop_thread_safe_call_sync(_nativeHandler, (IntPtr)id);
50 public static IntPtr AddTimer(double interval, Func<bool> handler)
52 int id = RegistHandler(handler);
53 return Interop.Ecore.ecore_timer_add(interval, _nativeHandler, (IntPtr)id);
56 public static void RemoveTimer(IntPtr id)
58 int taskId = (int)Interop.Ecore.ecore_timer_del(id);
59 _taskMap.Remove(taskId);
62 static int RegistHandler(Func<bool> task)
67 taskId = _newTaskId++;
69 _taskMap[taskId] = task;
73 static bool NativeHandler(IntPtr user_data)
75 int task_id = (int)user_data;
76 Func<bool> userAction = null;
77 _taskMap.TryGetValue(task_id, out userAction);
78 if (userAction != null)
80 _taskMap.Remove(task_id);