mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-27 00:07:58 +01:00
* touching publish.yml * Moved Examples into ./Examples * Moved Benchmarks into ./Tests * Moved Benchmarks into ./Tests * Moved UICatalog into ./Examples * Moved UICatalog into ./Examples 2 * Moved tests into ./Tests * Updated nuget
61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using System;
|
|
using System.Reactive.Concurrency;
|
|
using System.Reactive.Disposables;
|
|
using Terminal.Gui;
|
|
|
|
namespace ReactiveExample;
|
|
|
|
public class TerminalScheduler : LocalScheduler
|
|
{
|
|
public static readonly TerminalScheduler Default = new ();
|
|
private TerminalScheduler () { }
|
|
|
|
public override IDisposable Schedule<TState> (
|
|
TState state,
|
|
TimeSpan dueTime,
|
|
Func<IScheduler, TState, IDisposable> action
|
|
)
|
|
{
|
|
IDisposable PostOnMainLoop ()
|
|
{
|
|
var composite = new CompositeDisposable (2);
|
|
var cancellation = new CancellationDisposable ();
|
|
|
|
Application.Invoke (
|
|
() =>
|
|
{
|
|
if (!cancellation.Token.IsCancellationRequested)
|
|
{
|
|
composite.Add (action (this, state));
|
|
}
|
|
}
|
|
);
|
|
composite.Add (cancellation);
|
|
|
|
return composite;
|
|
}
|
|
|
|
IDisposable PostOnMainLoopAsTimeout ()
|
|
{
|
|
var composite = new CompositeDisposable (2);
|
|
|
|
object timeout = Application.AddTimeout (
|
|
dueTime,
|
|
() =>
|
|
{
|
|
composite.Add (action (this, state));
|
|
|
|
return false;
|
|
}
|
|
);
|
|
composite.Add (Disposable.Create (() => Application.RemoveTimeout (timeout)));
|
|
|
|
return composite;
|
|
}
|
|
|
|
return dueTime == TimeSpan.Zero
|
|
? PostOnMainLoop ()
|
|
: PostOnMainLoopAsTimeout ();
|
|
}
|
|
}
|