File tree Expand file tree Collapse file tree
EnumerableAsyncProcessor.Example Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,6 +12,12 @@ namespace EnumerableAsyncProcessor.Example;
1212/// <summary>
1313/// Examples demonstrating proper disposal patterns for EnumerableAsyncProcessor objects.
1414/// This addresses the common question: "How/when to correctly dispose the resulting processor objects?"
15+ ///
16+ /// QUICK ANSWER: Always dispose processor objects using 'await using' or manual disposal!
17+ ///
18+ /// The key pattern is:
19+ /// ❌ BAD: var processor = items.SelectAsync(...).ProcessInParallel(); // Never disposed!
20+ /// ✅ GOOD: await using var processor = items.SelectAsync(...).ProcessInParallel(); // Auto-disposed!
1521/// </summary>
1622public static class DisposalExample
1723{
Original file line number Diff line number Diff line change @@ -390,3 +390,36 @@ var transformedResults = await asyncEnumerable.ProcessInParallel(async x => awai
390390```
391391
392392The disposal guidance above applies when you're working with the processor objects directly (using the builder pattern).
393+
394+ ## Quick Reference: Disposal Patterns
395+
396+ ### ❌ INCORRECT (Resource Leak)
397+ ``` csharp
398+ // DON'T DO THIS - Never disposed!
399+ var processor = input .SelectAsync (transform , token ).ProcessInParallel ();
400+ return processor .GetResultsAsyncEnumerable ();
401+ ```
402+
403+ ### ✅ CORRECT Patterns
404+
405+ #### Option 1: Await Using (Recommended)
406+ ``` csharp
407+ await using var processor = input .SelectAsync (transform , token ).ProcessInParallel ();
408+ return await processor .GetResultsAsync ();
409+ ```
410+
411+ #### Option 2: Manual Disposal
412+ ``` csharp
413+ var processor = input .SelectAsync (transform , token ).ProcessInParallel ();
414+ try {
415+ return await processor .GetResultsAsync ();
416+ } finally {
417+ await processor .DisposeAsync ();
418+ }
419+ ```
420+
421+ #### Option 3: Extension Methods (Auto-Disposal)
422+ ``` csharp
423+ // These handle disposal internally
424+ var results = await asyncEnumerable .ProcessInParallel (transform );
425+ ```
You can’t perform that action at this time.
0 commit comments