Skip to content

Commit 4922989

Browse files
authored
Merge pull request #774 from Excel-DNA/QueueAsMacroTask
Added ExcelAsyncUtil.QueueAsMacroTask()
2 parents 347fa5b + 736824b commit 4922989

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Source/ExcelDna.Integration/ExcelAsyncUtil.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public enum ExcelObservableOptions
5252

5353
public static class ExcelAsyncUtil
5454
{
55+
internal struct VoidTaskResult { }
56+
5557
[Obsolete("ExcelAsyncUtil.Initialize is no longer required. The call can be removed.")]
5658
public static void Initialize() { }
5759
[Obsolete("ExcelAsyncUtil.Uninitialize is no longer required. The call can be removed.")]
@@ -143,6 +145,37 @@ public static void QueueAsMacro(SendOrPostCallback callback, object state)
143145
SynchronizationManager.RunMacroSynchronization.RunAsMacroAsync(callback, state);
144146
}
145147

148+
public static Task QueueMacroTask(string macroName)
149+
{
150+
return QueueAsMacroTask(RunMacro, macroName);
151+
}
152+
153+
public static Task QueueAsMacroTask(ExcelAction action)
154+
{
155+
SendOrPostCallback callback = delegate { action(); };
156+
return QueueAsMacroTask(callback, null);
157+
}
158+
159+
public static Task QueueAsMacroTask(SendOrPostCallback callback, object state)
160+
{
161+
var tcs = new TaskCompletionSource<VoidTaskResult>();
162+
163+
QueueAsMacro((s) =>
164+
{
165+
try
166+
{
167+
callback(s);
168+
tcs.SetResult(new VoidTaskResult());
169+
}
170+
catch (Exception ex)
171+
{
172+
tcs.SetException(ex);
173+
}
174+
}, state);
175+
176+
return tcs.Task;
177+
}
178+
146179
static void RunMacro(object macroName)
147180
{
148181
XlCall.Excel(XlCall.xlcRun, macroName);

0 commit comments

Comments
 (0)