| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace Avalonia.Gtk3.Interop
- {
- static class GlibTimeout
- {
- static bool Handler(IntPtr data)
- {
- var handle = GCHandle.FromIntPtr(data);
- var cb = (Func<bool>) handle.Target;
- if (!cb())
- {
- handle.Free();
- return false;
- }
- return true;
- }
- private static readonly GCHandle PinnedHandle;
- private static readonly Native.D.timeout_callback PinnedHandler;
- static GlibTimeout()
- {
- PinnedHandler = Handler;
-
- }
- public static void Add(uint interval, Func<bool> callback)
- {
- var handle = GCHandle.Alloc(callback);
- Native.GTimeoutAdd(interval, PinnedHandler, GCHandle.ToIntPtr(handle));
- }
-
- public static void Add(int priority, uint interval, Func<bool> callback)
- {
- var handle = GCHandle.Alloc(callback);
- Native.GTimeoutAddFull(priority, interval, PinnedHandler, GCHandle.ToIntPtr(handle), IntPtr.Zero);
- }
- class Timer : IDisposable
- {
- public bool Stopped;
- public void Dispose()
- {
- Stopped = true;
- }
- }
- public static IDisposable StarTimer(uint interval, Action tick)
- {
- var timer = new Timer ();
- GlibTimeout.Add(interval,
- () =>
- {
- if (timer.Stopped)
- return false;
- tick();
- return !timer.Stopped;
- });
- return timer;
- }
- }
- }
|