-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdleAction.cs
82 lines (72 loc) · 2.39 KB
/
IdleAction.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// IdleAction.cs ActivistInvestor / Tony T.
/// Distributed under the terms of the MIT license
///
/// Repo source: https://github.com/ActivistInvestor/AcMgdUtility/blob/main/IdleAction.cs
///
using Autodesk.AutoCAD.ApplicationServices;
using System;
public class IdleAction
{
Action action;
bool quiescentRequired = false;
bool documentRequired = false;
static DocumentCollection docs = Application.DocumentManager;
/// <summary>
/// Creates a new instance of an IdleAction
/// </summary>
/// <param name="action">The action to execute on the
/// next raising of the Application.Idle event</param>
/// <param name="quiescent">A value indicating if
/// invocation of the action is deferred until there
/// is an active document and it is in a quiescent state.
/// <param name="document">A value indicating if invocation
/// of the action should be deferred until there is an
/// active document.
/// If <paramref name="quiescent"/> is true, this condition
/// is not evaluated and is effectively true.</param>
/// <exception cref="ArgumentNullException"></exception>
public IdleAction(Action action, bool quiescent = false, bool document = true)
{
this.action = action;
if(action != null)
{
this.documentRequired = document;
this.quiescentRequired = quiescent;
Application.Idle += idle;
}
}
public static IdleAction OnIdle(Action action, bool quiescent = false, bool document = true)
{
return new IdleAction(action);
}
public static IdleAction OnIdle(bool quiescent, bool document, Action action)
{
return new IdleAction(action, quiescent, document);
}
public static IdleAction OnIdle(bool quiescent, Action action)
{
return new IdleAction(action, quiescent);
}
public static IdleAction Invoke(Action action)
{
return new IdleAction(action);
}
bool CanInvoke
{
get
{
Document doc = docs.MdiActiveDocument;
return (doc == null) ? !documentRequired
: !quiescentRequired || doc.Editor.IsQuiescent;
}
}
private void idle(object sender, EventArgs e)
{
if(action != null && CanInvoke)
{
Application.Idle -= idle;
try { action(); }
finally { action = null; }
}
}
}