{"record":{"id":"5ece49b8f5bfdb0e","repo":"HangfireIO/Hangfire","slug":"threadcount","errorCode":null,"errorMessage":"threadCount","messagePattern":"threadCount","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Hangfire.Core/Processing/BackgroundTaskScheduler.cs","lineNumber":212,"sourceCode":"            // Since we want to execute as much tasks as possible on our dedicated threads,\n            // we allow to inline only requests from the current scheduler, i.e. just to save\n            // some time, since no queueing will be involved.\n\n            if (!_ourThreadIds.Contains(Environment.CurrentManagedThreadId)) return false;\n\n            return TryExecuteTask(task);\n        }\n\n        /// <inheritdoc />\n        protected override IEnumerable<Task> GetScheduledTasks()\n        {\n            ThrowIfDisposed();\n            return _queue.ToArray();\n        }\n\n        private static IEnumerable<Thread> DefaultThreadFactory(ThreadStart threadStart, int threadCount)\n        {\n            if (threadCount <= 0) throw new ArgumentOutOfRangeException(nameof(threadCount));\n            var threads = new Thread[threadCount];\n\n            for (var i = 0; i < threadCount; i++)\n            {\n                threads[i] = new Thread(threadStart)\n                {\n                    Name = $\"BackgroundThread #{i + 1}\",\n                    IsBackground = true,\n                };\n            }\n\n            return threads;\n        }\n\n        private static void DefaultExceptionHandler(Exception exception)\n        {\n#if !NETSTANDARD1_3\n            Trace.WriteLine(\"An unhandled exception occurred: \" + exception);","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.Core/Processing/BackgroundTaskScheduler.cs#L194-L230","documentation":"Thrown by BackgroundTaskScheduler.DefaultThreadFactory (private) when threadCount is less than or equal to zero. Reached through the public BackgroundTaskScheduler(int threadCount) constructor; threadCount sizes the dedicated thread pool and MaximumConcurrencyLevel, so a non-positive value is invalid.","triggerScenarios":"Calling `new BackgroundTaskScheduler(0)` or with a negative threadCount. Reached when Environment.ProcessorCount is unexpectedly 0 or when a user-supplied concurrency value is non-positive.","commonSituations":"Config binds WorkerCount/threadCount to 0; a UI allows zero; arithmetic that produces a negative; containerised environment where processor count resolves oddly.","solutions":["Pass a threadCount >= 1; prefer Environment.ProcessorCount via the parameterless ctor.","Clamp configured values: `Math.Max(1, configuredCount)`.","Validate config at load time and reject non-positive counts early."],"exampleFix":"// before\nvar scheduler = new BackgroundTaskScheduler(0);\n\n// after\nvar scheduler = new BackgroundTaskScheduler(Math.Max(1, Environment.ProcessorCount));","handlingStrategy":"validation","validationCode":"int count = Math.Max(1, configuredThreadCount);\nvar scheduler = new BackgroundTaskScheduler(count);","typeGuard":null,"tryCatchPattern":"try { var s = new BackgroundTaskScheduler(configuredCount); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == nameof(configuredCount))\n{ configuredCount = Environment.ProcessorCount; /* retry */ }","preventionTips":["Clamp thread counts to >= 1 at the configuration boundary.","Prefer the parameterless ctor (uses Environment.ProcessorCount).","Reject zero/negative counts in config validation."],"tags":["hangfire","argument-validation","task-scheduler","configuration","threading"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}