10 changed files with 88 additions and 24 deletions
@ -0,0 +1,58 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Perspex.Platform; |
|||
|
|||
namespace Perspex.Threading |
|||
{ |
|||
public class SingleThreadDispatcher : Dispatcher |
|||
{ |
|||
class ThreadingInterface : IPlatformThreadingInterface |
|||
{ |
|||
private AutoResetEvent _evnt = new AutoResetEvent(false); |
|||
private JobRunner _timerJobRunner; |
|||
|
|||
public ThreadingInterface() |
|||
{ |
|||
_timerJobRunner = new JobRunner(this); |
|||
} |
|||
|
|||
public void RunLoop(CancellationToken cancellationToken) |
|||
{ |
|||
while (!cancellationToken.IsCancellationRequested) |
|||
{ |
|||
_evnt.WaitOne(); |
|||
if (cancellationToken.IsCancellationRequested) |
|||
return; |
|||
Signaled?.Invoke(); |
|||
_timerJobRunner.RunJobs(); |
|||
} |
|||
} |
|||
|
|||
public IDisposable StartTimer(TimeSpan interval, Action tick) |
|||
=> PerspexLocator.Current.GetService<IPclPlatformWrapper>().StartSystemTimer(interval, |
|||
() => _timerJobRunner.Post(tick, DispatcherPriority.Normal)); |
|||
|
|||
public void Signal() => _evnt.Set(); |
|||
|
|||
public event Action Signaled; |
|||
} |
|||
|
|||
public SingleThreadDispatcher() : base(new ThreadingInterface()) |
|||
{ |
|||
} |
|||
|
|||
public static Dispatcher StartNew(CancellationToken token) |
|||
{ |
|||
var dispatcher = new SingleThreadDispatcher(); |
|||
PerspexLocator.Current.GetService<IPclPlatformWrapper>().PostThreadPoolItem(() => |
|||
{ |
|||
dispatcher.MainLoop(token); |
|||
}); |
|||
return dispatcher; |
|||
} |
|||
} |
|||
} |
|||
Binary file not shown.
Loading…
Reference in new issue