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.Reactive.Disposables;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.Threading; using Avalonia.Threading;
@ -197,15 +198,42 @@ namespace Avalonia.Windowing
public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick) public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick)
{ {
// TODO: Need a way to cancel a timer when Dispose is called. return new WinitTimer(new Timer(delegate
{
// var x = new TimerDel(tick); var tcs = new TaskCompletionSource<int>();
// timerTickers.Add(x);
// _eventsLoop.RunTimer(x); Dispatcher.UIThread.Post(() =>
return Disposable.Create(() => { {
// timerTickers.Remove(x); 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