|
7 | 7 |
|
8 | 8 | namespace Shuttle.Core.Threading; |
9 | 9 |
|
10 | | -public class ProcessorThread |
| 10 | +public class ProcessorThread : IProcessorThreadContext |
11 | 11 | { |
12 | 12 | private readonly CancellationTokenSource _cancellationTokenSource = new(); |
13 | 13 | private readonly ProcessorThreadOptions _processorThreadOptions; |
14 | 14 |
|
15 | 15 | private readonly Dictionary<string, object> _state = new(); |
16 | | - private ProcessorThreadEventArgs _eventArgs = new("unknown", -1); |
| 16 | + private readonly ProcessorThreadEventArgs _eventArgs; |
17 | 17 |
|
18 | 18 | private bool _started; |
19 | | - private Thread? _thread; |
| 19 | + private readonly Thread _thread; |
20 | 20 |
|
21 | 21 | public ProcessorThread(string name, IProcessor processor, ProcessorThreadOptions processorThreadOptions) |
22 | 22 | { |
23 | 23 | Name = Guard.AgainstNull(name); |
24 | 24 | Processor = Guard.AgainstNull(processor); |
25 | 25 | _processorThreadOptions = Guard.AgainstNull(processorThreadOptions); |
26 | 26 | 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); |
27 | 36 | } |
28 | 37 |
|
29 | 38 | public CancellationToken CancellationToken { get; } |
@@ -63,15 +72,6 @@ public async Task StartAsync() |
63 | 72 | return; |
64 | 73 | } |
65 | 74 |
|
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 | | - |
75 | 75 | _thread.Start(); |
76 | 76 |
|
77 | 77 | while (!_thread.IsAlive && !CancellationToken.IsCancellationRequested) |
@@ -126,7 +126,7 @@ private async void Work() |
126 | 126 |
|
127 | 127 | try |
128 | 128 | { |
129 | | - await Processor.ExecuteAsync(CancellationToken); |
| 129 | + await Processor.ExecuteAsync(this, CancellationToken); |
130 | 130 | } |
131 | 131 | catch (OperationCanceledException) |
132 | 132 | { |
|
0 commit comments