forked from gui-cs/Terminal.Gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalScheduler.cs
More file actions
60 lines (50 loc) · 1.96 KB
/
TerminalScheduler.cs
File metadata and controls
60 lines (50 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 ();
}
}