Skip to content

Commit 8a2fb25

Browse files
committed
Added IProcessorThreadContext to expose state to the processor.
1 parent 53d0cee commit 8a2fb25

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

Shuttle.Core.Threading.Tests/MockProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public MockProcessor(TimeSpan executionDuration)
1515

1616
public int ExecutionCount { get; private set; }
1717

18-
public async Task ExecuteAsync(CancellationToken cancellationToken)
18+
public async Task ExecuteAsync(IProcessorThreadContext processorThreadContext, CancellationToken cancellationToken)
1919
{
2020
await Task.Delay(_executionDuration, cancellationToken).ConfigureAwait(false);
2121
ExecutionCount++;

Shuttle.Core.Threading/IProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ namespace Shuttle.Core.Threading;
55

66
public interface IProcessor
77
{
8-
Task ExecuteAsync(CancellationToken cancellationToken = default);
8+
Task ExecuteAsync(IProcessorThreadContext processorThreadContext, CancellationToken cancellationToken = default);
99
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Shuttle.Core.Threading;
2+
3+
public interface IProcessorThreadContext
4+
{
5+
object? GetState(string key);
6+
}

Shuttle.Core.Threading/ProcessorThread.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,32 @@
77

88
namespace Shuttle.Core.Threading;
99

10-
public class ProcessorThread
10+
public class ProcessorThread : IProcessorThreadContext
1111
{
1212
private readonly CancellationTokenSource _cancellationTokenSource = new();
1313
private readonly ProcessorThreadOptions _processorThreadOptions;
1414

1515
private readonly Dictionary<string, object> _state = new();
16-
private ProcessorThreadEventArgs _eventArgs = new("unknown", -1);
16+
private readonly ProcessorThreadEventArgs _eventArgs;
1717

1818
private bool _started;
19-
private Thread? _thread;
19+
private readonly Thread _thread;
2020

2121
public ProcessorThread(string name, IProcessor processor, ProcessorThreadOptions processorThreadOptions)
2222
{
2323
Name = Guard.AgainstNull(name);
2424
Processor = Guard.AgainstNull(processor);
2525
_processorThreadOptions = Guard.AgainstNull(processorThreadOptions);
2626
CancellationToken = _cancellationTokenSource.Token;
27+
28+
_thread = new(Work) { Name = Name };
29+
30+
_thread.TrySetApartmentState(ApartmentState.MTA);
31+
32+
_thread.IsBackground = _processorThreadOptions.IsBackground;
33+
_thread.Priority = _processorThreadOptions.Priority;
34+
35+
_eventArgs = new(Name, _thread.ManagedThreadId);
2736
}
2837

2938
public CancellationToken CancellationToken { get; }
@@ -63,15 +72,6 @@ public async Task StartAsync()
6372
return;
6473
}
6574

66-
_thread = new(Work) { Name = Name };
67-
68-
_thread.TrySetApartmentState(ApartmentState.MTA);
69-
70-
_thread.IsBackground = _processorThreadOptions.IsBackground;
71-
_thread.Priority = _processorThreadOptions.Priority;
72-
73-
_eventArgs = new(Name, _thread.ManagedThreadId);
74-
7575
_thread.Start();
7676

7777
while (!_thread.IsAlive && !CancellationToken.IsCancellationRequested)
@@ -126,7 +126,7 @@ private async void Work()
126126

127127
try
128128
{
129-
await Processor.ExecuteAsync(CancellationToken);
129+
await Processor.ExecuteAsync(this, CancellationToken);
130130
}
131131
catch (OperationCanceledException)
132132
{

0 commit comments

Comments
 (0)