mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +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
This commit is contained in:
60
Examples/ReactiveExample/TerminalScheduler.cs
Normal file
60
Examples/ReactiveExample/TerminalScheduler.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
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 ();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user