From 0f8662814778910f79569b6adc36ad20627f42ef Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Fri, 10 Jan 2025 18:31:26 +0100 Subject: [PATCH] Long-running tasks using factory --- src/Core/src/Eventuous.Shared/Tools/TaskRunner.cs | 2 +- .../Eventuous.Subscriptions/Channels/ChannelWorkerBase.cs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Core/src/Eventuous.Shared/Tools/TaskRunner.cs b/src/Core/src/Eventuous.Shared/Tools/TaskRunner.cs index be729a0d..40717fda 100644 --- a/src/Core/src/Eventuous.Shared/Tools/TaskRunner.cs +++ b/src/Core/src/Eventuous.Shared/Tools/TaskRunner.cs @@ -9,7 +9,7 @@ public sealed class TaskRunner(Func taskFactory) : IDis Task? _runner; public TaskRunner Start() { - _runner = Task.Run(Run); + _runner = Task.Factory.StartNew(Run, _stopSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); return this; diff --git a/src/Core/src/Eventuous.Subscriptions/Channels/ChannelWorkerBase.cs b/src/Core/src/Eventuous.Subscriptions/Channels/ChannelWorkerBase.cs index ad5705da..53dbf6bb 100644 --- a/src/Core/src/Eventuous.Subscriptions/Channels/ChannelWorkerBase.cs +++ b/src/Core/src/Eventuous.Subscriptions/Channels/ChannelWorkerBase.cs @@ -21,7 +21,10 @@ public ValueTask Write(T element, CancellationToken cancellationToken) protected ChannelWorkerBase(Channel channel, Func processor, int concurrencyLevel, bool throwOnFull = false) { _channel = channel; _throwOnFull = throwOnFull; - _readerTasks = Enumerable.Range(0, concurrencyLevel).Select(_ => Task.Run(() => processor(_cts.Token))).ToArray(); + + _readerTasks = Enumerable.Range(0, concurrencyLevel) + .Select(_ => Task.Factory.StartNew(() => processor(_cts.Token), _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default)) + .ToArray(); } public async ValueTask DisposeAsync() {