Skip to content

Commit 846679d

Browse files
Copilotthomhurst
andcommitted
Add quick reference section and validate disposal patterns work
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
1 parent 90ec67c commit 846679d

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

EnumerableAsyncProcessor.Example/DisposalExample.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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>
1622
public static class DisposalExample
1723
{

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,36 @@ var transformedResults = await asyncEnumerable.ProcessInParallel(async x => awai
390390
```
391391

392392
The 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+
```

0 commit comments

Comments
 (0)