|
| 1 | +// To add the async / batch running into your code: |
| 2 | +// 1. Add the whole "AsyncBatchUtil" class from this file into your project. |
| 3 | +// 2. Create your own batch runner function, similar to the "RunBatch" method below. |
| 4 | +// 3. Create an instance of "AsyncBatchUtil" somewhere in your code (it's called "BatchRunner" below), passing in the batch parameters and batch runner from step 2. |
| 5 | +// 4. Create your worksheet functions like "SlowFunction" below, which call "BatchRunner.Run(...)" to run async as part of a batch. |
| 6 | + |
1 | 7 | using System; |
2 | 8 | using System.Collections.Generic; |
3 | 9 | using System.Threading; |
|
9 | 15 |
|
10 | 16 | namespace GeneralTestsCS |
11 | 17 | { |
12 | | - |
13 | | - public static class AysyncBatchExample |
| 18 | + public static class AsyncBatchExample |
14 | 19 | { |
15 | | - // 1. Create an instance on the AsyncBatchUtil, passing in some parameters and the btah running function. |
| 20 | + // Step 3. from the list. |
16 | 21 | static readonly AsyncBatchUtil BatchRunner = new AsyncBatchUtil(1000, TimeSpan.FromMilliseconds(250), RunBatch); |
17 | 22 |
|
| 23 | + // Step 2. from the list. |
18 | 24 | // This function will be called for each batch, on a ThreadPool thread. |
19 | 25 | // Each AsyncCall contains the function name and arguments passed from the function. |
20 | 26 | // The List<object> returned by the Task must contain the results, corresponding to the calls list. |
@@ -42,21 +48,15 @@ static async Task<List<object>> RunBatch(List<AsyncBatchUtil.AsyncCall> calls) |
42 | 48 | return results; |
43 | 49 | } |
44 | 50 |
|
| 51 | + // Step 4. from the list. |
45 | 52 | public static object SlowFunction(string code, int value) |
46 | 53 | { |
47 | 54 | return BatchRunner.Run("SlowFunction", code, value); |
48 | 55 | } |
49 | 56 | } |
50 | 57 |
|
51 | | - |
| 58 | + // Step 1. from the list. |
52 | 59 | // This is the main helper class for supporting batched async calls |
53 | | - // To use: |
54 | | - // 1. Create an instance of AsyncBatchUtil, passing in a Func<List<AsyncCall>, Task<List<object>>> to run each batch. |
55 | | - // 2. Call from inside a batched function as: |
56 | | - // public static object SlowFunction(string code, int value) |
57 | | - // { |
58 | | - // return BatchRunner.Run("SlowFunction", code, value); |
59 | | - // } |
60 | 60 | public class AsyncBatchUtil |
61 | 61 | { |
62 | 62 | // Represents a single function call in a batch |
|
0 commit comments