Browse Source

Implement Timer support

thread-issue-repro
Nelson Carrillo 8 years ago
parent
commit
2d4630d23f
  1. 44
      src/Avalonia.Windowing/WindowingPlatform.cs

44
src/Avalonia.Windowing/WindowingPlatform.cs

@ -5,6 +5,7 @@ using System.Linq;
using System.Reactive.Disposables;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.Threading;
@ -197,15 +198,42 @@ namespace Avalonia.Windowing
public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick)
{
// TODO: Need a way to cancel a timer when Dispose is called.
// var x = new TimerDel(tick);
// timerTickers.Add(x);
return new WinitTimer(new Timer(delegate
{
var tcs = new TaskCompletionSource<int>();
// _eventsLoop.RunTimer(x);
return Disposable.Create(() => {
// timerTickers.Remove(x);
});
Dispatcher.UIThread.Post(() =>
{
try
{
tick();
}
finally
{
tcs.SetResult(0);
}
});
tcs.Task.Wait();
}, null, TimeSpan.Zero, interval));
}
}
public class WinitTimer : IDisposable
{
private readonly IDisposable _timer;
private readonly GCHandle _gcHandle;
public WinitTimer(IDisposable timer)
{
_timer = timer;
_gcHandle = GCHandle.Alloc(_timer);
}
public void Dispose()
{
_gcHandle.Free();
_timer.Dispose();
}
}
}

Loading…
Cancel
Save