[英]How to await an ExcelUtilAsync task?
Im using ExcelDNA for an addin. 我使用ExcelDNA作为插件。 I have a few tasks running asychronously using ExcelUtilAsync.Run, but when they're all done, I want to run a function formatting all their output to return to excel.
我有一些任务使用ExcelUtilAsync.Run异步运行,但是当它们全部完成后,我想运行一个格式化所有输出格式以返回excel的函数。 I tried using a Barrier, but it cannot return a value (I think, I'm new to c#).
我尝试使用Barrier,但它无法返回值(我认为,我是C#的新手)。 How do I await/join these tasks?
我如何等待/加入这些任务?
There's lots of ways to do that... Depending on how/when you want to wait for the completion. 有很多方法可以执行此操作...取决于您要等待何时/如何完成。
A very simple way to do that (assuming you know how many tasks will run) would be to use a CountdownEvent
. 一个简单的方法(假设您知道要运行多少个任务)是使用
CountdownEvent
。 Something like this: 像这样:
// Define how many tasks will be run via ExcelAsyncUtil
var countdownEvent = new CountdownEvent(2);
ExcelAsyncUtil.Run("Task1", null, handle =>
{
try
{
// Do work for Task 1
}
finally
{
countdownEvent.Signal();
}
});
ExcelAsyncUtil.Run("Task2", null, handle =>
{
try
{
// Do work for Task 2
}
finally
{
countdownEvent.Signal();
}
});
// Wait for all of them to finish (blocking)
countdownEvent.Wait();
// All async tasks have been completed
// (...)
You could also look at the AsyncAwaitMacro example in Excel-DNA: https://github.com/Excel-DNA/Samples/tree/master/AsyncAwaitMacro 您还可以查看Excel-DNA中的AsyncAwaitMacro示例: https : //github.com/Excel-DNA/Samples/tree/master/AsyncAwaitMacro
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.