|
1 |
| -using System.Collections.Generic; |
2 |
| -using System.Diagnostics; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
3 | 3 | using System.Runtime.InteropServices;
|
4 | 4 | using System.Threading;
|
5 | 5 | using Microsoft.Office.Interop.OneNote;
|
6 | 6 |
|
7 | 7 | namespace Odotocodot.OneNote.Linq
|
8 | 8 | {
|
9 | 9 | /// <summary>
|
10 |
| - /// A static wrapper class around the <see cref="Application"/> class, allowing for easy acquirement and |
11 |
| - /// release of a OneNote COM object, whilst avoiding duplicate instances. In addition to exposing the |
| 10 | + /// A static wrapper class around the <see cref="Application"/> class, allowing for <see cref="Lazy{T}">lazy</see> acquirement and |
| 11 | + /// release of a OneNote COM object. In addition to exposing the |
12 | 12 | /// <see href="https://learn.microsoft.com/en-us/office/client-developer/onenote/application-interface-onenote">OneNote's API</see>
|
13 | 13 | /// </summary>
|
14 | 14 | /// <remarks>A <see cref="Application">OneNote COM object</see> is required to access any of the OneNote API.</remarks>
|
15 | 15 | public static class OneNoteApplication
|
16 | 16 | {
|
17 |
| - private static Application oneNote; |
18 |
| - private static bool hasComInstance = false; |
| 17 | + private static Lazy<Application> lazyOneNote = new (() => new Application(), LazyThreadSafetyMode.ExecutionAndPublication); |
| 18 | + private static Application OneNote => lazyOneNote.Value; |
19 | 19 | /// <summary>
|
20 | 20 | /// Indicates whether the class has a usable <see cref="Application">COM instance</see>.
|
21 | 21 | /// </summary>
|
22 |
| - /// <remarks>When <see langword="true"/> a OneNote process should be visible in the Task Manager.</remarks> |
| 22 | + /// <remarks>When <see langword="true"/> a "Microsoft OneNote" process should be visible in the Task Manager.</remarks> |
23 | 23 | /// <seealso cref="Init"/>
|
24 |
| - /// <seealso cref="ReleaseComInstance"/> |
25 |
| - public static bool HasComInstance => hasComInstance; |
| 24 | + /// <seealso cref="ReleaseComObject"/> |
| 25 | + public static bool HasComObject => lazyOneNote.IsValueCreated; |
26 | 26 |
|
27 | 27 | /// <summary>
|
28 |
| - /// Initialises the static class by acquiring a <see cref="Application">OneNote COM object</see>. |
| 28 | + /// Forceable initialises the static class by acquiring a <see cref="Application">OneNote COM object</see>. |
29 | 29 | /// </summary>
|
30 | 30 | /// <exception cref="COMException">Thrown if an error occurred when trying to get the
|
31 | 31 | /// <see cref="Application">OneNote COM object</see> or the number of attempts in doing
|
32 | 32 | /// so exceeded the limit.</exception>
|
33 |
| - /// <seealso cref="HasComInstance"/> |
34 |
| - /// <seealso cref="ReleaseComInstance"/> |
| 33 | + /// <seealso cref="HasComObject"/> |
| 34 | + /// <seealso cref="ReleaseComObject"/> |
35 | 35 | public static void Init()
|
36 | 36 | {
|
37 |
| - int attempt = 0; |
38 |
| - |
39 |
| - while (!hasComInstance) |
| 37 | + if (!lazyOneNote.IsValueCreated) |
40 | 38 | {
|
41 |
| - try |
42 |
| - { |
43 |
| - oneNote = new Application(); |
44 |
| - hasComInstance = oneNote != null && Marshal.IsComObject(oneNote); |
45 |
| - } |
46 |
| - catch (COMException ex) when (attempt++ < 3) |
47 |
| - { |
48 |
| - Trace.TraceError(ex.Message); |
49 |
| - Thread.Sleep(100); |
50 |
| - } |
51 |
| - catch (COMException ex) when (attempt == 3) |
52 |
| - { |
53 |
| - throw new COMException("Unable to acquire a OneNote COM object", ex); |
54 |
| - } |
| 39 | + _ = OneNote; |
55 | 40 | }
|
56 | 41 | }
|
57 | 42 | /// <inheritdoc cref="OneNoteParser.GetNotebooks(IApplication)"/>
|
58 |
| - public static IEnumerable<OneNoteNotebook> GetNotebooks() |
59 |
| - { |
60 |
| - Init(); |
61 |
| - return OneNoteParser.GetNotebooks(oneNote); |
62 |
| - } |
| 43 | + public static IEnumerable<OneNoteNotebook> GetNotebooks() => OneNoteParser.GetNotebooks(OneNote); |
| 44 | + |
63 | 45 | /// <inheritdoc cref="OneNoteParser.OpenInOneNote(IApplication, IOneNoteItem)"/>
|
64 |
| - public static void OpenInOneNote(IOneNoteItem item) |
65 |
| - { |
66 |
| - Init(); |
67 |
| - OneNoteParser.OpenInOneNote(oneNote, item); |
68 |
| - } |
| 46 | + public static void OpenInOneNote(IOneNoteItem item) => OneNoteParser.OpenInOneNote(OneNote, item); |
| 47 | + |
69 | 48 | /// <inheritdoc cref="OneNoteParser.SyncItem(IApplication, IOneNoteItem)"/>
|
70 |
| - public static void SyncItem(IOneNoteItem item) |
71 |
| - { |
72 |
| - Init(); |
73 |
| - OneNoteParser.SyncItem(oneNote, item); |
74 |
| - } |
| 49 | + public static void SyncItem(IOneNoteItem item) => OneNoteParser.SyncItem(OneNote, item); |
| 50 | + |
75 | 51 | /// <inheritdoc cref="OneNoteParser.GetPageContent(IApplication, OneNotePage)"/>
|
76 |
| - public static string GetPageContent(OneNotePage page) |
77 |
| - { |
78 |
| - Init(); |
79 |
| - return OneNoteParser.GetPageContent(oneNote, page); |
80 |
| - } |
| 52 | + public static string GetPageContent(OneNotePage page)=> OneNoteParser.GetPageContent(OneNote, page); |
81 | 53 |
|
82 | 54 | /// <inheritdoc cref="OneNoteParser.FindPages(IApplication, string)"/>
|
83 |
| - public static IEnumerable<OneNotePage> FindPages(string search) |
84 |
| - { |
85 |
| - Init(); |
86 |
| - return OneNoteParser.FindPages(oneNote, search); |
87 |
| - } |
| 55 | + public static IEnumerable<OneNotePage> FindPages(string search) => OneNoteParser.FindPages(OneNote, search); |
| 56 | + |
88 | 57 | /// <inheritdoc cref="OneNoteParser.FindPages(IApplication, string, IOneNoteItem)"/>
|
89 |
| - public static IEnumerable<OneNotePage> FindPages(IOneNoteItem scope, string search) |
90 |
| - { |
91 |
| - Init(); |
92 |
| - return OneNoteParser.FindPages(oneNote, search, scope); |
93 |
| - } |
| 58 | + public static IEnumerable<OneNotePage> FindPages(IOneNoteItem scope, string search) => OneNoteParser.FindPages(OneNote, search, scope); |
94 | 59 |
|
95 | 60 | /// <inheritdoc cref="OneNoteParser.GetDefaultNotebookLocation(IApplication)"/>
|
96 |
| - public static string GetDefaultNotebookLocation() |
97 |
| - { |
98 |
| - Init(); |
99 |
| - return OneNoteParser.GetDefaultNotebookLocation(oneNote); |
100 |
| - } |
| 61 | + public static string GetDefaultNotebookLocation() => OneNoteParser.GetDefaultNotebookLocation(OneNote); |
101 | 62 |
|
102 | 63 | /// <inheritdoc cref="OneNoteParser.CreateQuickNote(IApplication, bool)"/>
|
103 |
| - public static void CreateQuickNote() |
104 |
| - { |
105 |
| - Init(); |
106 |
| - OneNoteParser.CreateQuickNote(oneNote, true); |
107 |
| - } |
| 64 | + public static void CreateQuickNote() => OneNoteParser.CreateQuickNote(OneNote, true); |
108 | 65 |
|
109 | 66 | /// <inheritdoc cref="OneNoteParser.CreatePage(IApplication, OneNoteSection, string, bool)"/>
|
110 |
| - public static void CreatePage(OneNoteSection section, string pageTitle) |
111 |
| - { |
112 |
| - Init(); |
113 |
| - OneNoteParser.CreatePage(oneNote, section, pageTitle, true); |
114 |
| - } |
| 67 | + public static void CreatePage(OneNoteSection section, string pageTitle) => OneNoteParser.CreatePage(OneNote, section, pageTitle, true); |
| 68 | + |
115 | 69 | /// <inheritdoc cref="OneNoteParser.CreateSection(IApplication, OneNoteSectionGroup, string, bool)"/>
|
116 |
| - public static void CreateSection(OneNoteSectionGroup parent, string sectionName) |
117 |
| - { |
118 |
| - Init(); |
119 |
| - OneNoteParser.CreateSection(oneNote, parent, sectionName, true); |
120 |
| - } |
| 70 | + public static void CreateSection(OneNoteSectionGroup parent, string sectionName) => OneNoteParser.CreateSection(OneNote, parent, sectionName, true); |
| 71 | + |
121 | 72 | /// <inheritdoc cref="OneNoteParser.CreateSection(IApplication, OneNoteNotebook, string, bool)"/>
|
122 |
| - public static void CreateSection(OneNoteNotebook parent, string sectionName) |
123 |
| - { |
124 |
| - Init(); |
125 |
| - OneNoteParser.CreateSection(oneNote, parent, sectionName, true); |
126 |
| - } |
| 73 | + public static void CreateSection(OneNoteNotebook parent, string sectionName) => OneNoteParser.CreateSection(OneNote, parent, sectionName, true); |
| 74 | + |
127 | 75 | /// <inheritdoc cref="OneNoteParser.CreateSectionGroup(IApplication, OneNoteSectionGroup, string, bool)"/>
|
128 |
| - public static void CreateSectionGroup(OneNoteSectionGroup parent, string sectionGroupName) |
129 |
| - { |
130 |
| - Init(); |
131 |
| - OneNoteParser.CreateSectionGroup(oneNote, parent, sectionGroupName, true); |
132 |
| - } |
| 76 | + public static void CreateSectionGroup(OneNoteSectionGroup parent, string sectionGroupName) => OneNoteParser.CreateSectionGroup(OneNote, parent, sectionGroupName, true); |
| 77 | + |
133 | 78 | /// <inheritdoc cref="OneNoteParser.CreateSectionGroup(IApplication, OneNoteNotebook, string, bool)"/>
|
134 |
| - public static void CreateSectionGroup(OneNoteNotebook parent, string sectionGroupName) |
135 |
| - { |
136 |
| - Init(); |
137 |
| - OneNoteParser.CreateSectionGroup(oneNote, parent, sectionGroupName, true); |
138 |
| - } |
| 79 | + public static void CreateSectionGroup(OneNoteNotebook parent, string sectionGroupName) => OneNoteParser.CreateSectionGroup(OneNote, parent, sectionGroupName, true); |
139 | 80 |
|
140 | 81 | /// <inheritdoc cref="OneNoteParser.CreateNotebook(IApplication, string, bool)"/>
|
141 |
| - public static void CreateNotebook(string notebookName) |
142 |
| - { |
143 |
| - Init(); |
144 |
| - OneNoteParser.CreateNotebook(oneNote, notebookName, true); |
145 |
| - } |
| 82 | + public static void CreateNotebook(string notebookName) => OneNoteParser.CreateNotebook(OneNote, notebookName, true); |
146 | 83 |
|
147 | 84 | /// <summary>
|
148 | 85 | /// Releases the <see cref="Application">OneNote COM object</see> freeing memory.
|
149 | 86 | /// </summary>
|
150 | 87 | /// <seealso cref="Init"/>
|
151 |
| - /// <seealso cref="HasComInstance"/> |
152 |
| - public static void ReleaseComInstance() |
| 88 | + /// <seealso cref="HasComObject"/> |
| 89 | + public static void ReleaseComObject() |
153 | 90 | {
|
154 |
| - if (hasComInstance) |
| 91 | + if (HasComObject) |
155 | 92 | {
|
156 |
| - Marshal.ReleaseComObject(oneNote); |
157 |
| - oneNote = null; |
158 |
| - hasComInstance = false; |
| 93 | + Marshal.ReleaseComObject(OneNote); |
| 94 | + lazyOneNote = new(() => new Application(), LazyThreadSafetyMode.ExecutionAndPublication); |
159 | 95 | }
|
160 | 96 | }
|
161 | 97 | }
|
|
0 commit comments