{"record":{"id":"d0815b8a210c91a9","repo":"dotnet/reactive","slug":"threadfactory-parameter-threadfactory","errorCode":null,"errorMessage":"threadFactory (Parameter 'threadFactory')","messagePattern":"threadFactory \\(Parameter 'threadFactory'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Concurrency/NewThreadScheduler.cs","lineNumber":39,"sourceCode":"        /// </summary>\n        public NewThreadScheduler()\n            : this(action => new Thread(action))\n        {\n        }\n\n        /// <summary>\n        /// Gets an instance of this scheduler that uses the default Thread constructor.\n        /// </summary>\n        public static NewThreadScheduler Default => Instance.Value;\n\n        /// <summary>\n        /// Creates an object that schedules each unit of work on a separate thread.\n        /// </summary>\n        /// <param name=\"threadFactory\">Factory function for thread creation.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"threadFactory\"/> is <c>null</c>.</exception>\n        public NewThreadScheduler(Func<ThreadStart, Thread> threadFactory)\n        {\n            _threadFactory = threadFactory ?? throw new ArgumentNullException(nameof(threadFactory));\n        }\n\n        /// <summary>\n        /// Schedules an action to be executed after dueTime.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state passed to the scheduled action.</typeparam>\n        /// <param name=\"state\">State passed to the action to be executed.</param>\n        /// <param name=\"action\">Action to be executed.</param>\n        /// <param name=\"dueTime\">Relative time after which to execute the action.</param>\n        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"action\"/> is <c>null</c>.</exception>\n        public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)\n        {\n            if (action == null)\n            {\n                throw new ArgumentNullException(nameof(action));\n            }\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/NewThreadScheduler.cs#L21-L57","documentation":"The NewThreadScheduler(Func<ThreadStart, Thread>) constructor throws ArgumentNullException when threadFactory is null. The factory is the core mechanism this scheduler uses to create a thread per unit of work, so without it the scheduler cannot function at all.","triggerScenarios":"Calling new NewThreadScheduler(null), or NewThreadScheduler.Default-style paths where a custom factory resolved from configuration/lambda is null.","commonSituations":"Building a scheduler from settings where the factory delegate is read as null; refactoring away Scheduler.NewThread internals; passing a method group that fails to bind (e.g. wrong overload) yielding null after a ternary.","solutions":["Pass a valid factory such as 'static ThreadStart ts => new Thread(ts)' to the constructor.","Use NewThreadScheduler.Default (or Scheduler.NewThread) instead of constructing manually.","Guard the factory argument with ArgumentNullException.ThrowIfNull before constructing.","Fix configuration resolution so the factory delegate is not null."],"exampleFix":"// before\nvar scheduler = new NewThreadScheduler(config.ThreadFactory); // null when unset\n// after\nvar scheduler = config.ThreadFactory is null\n    ? NewThreadScheduler.Default\n    : new NewThreadScheduler(config.ThreadFactory);","handlingStrategy":"validation","validationCode":"if (threadFactory == null)\n    threadFactory = ts => new Thread(ts); // or use NewThreadScheduler.Default","typeGuard":"bool HasThreadFactory(Func<ThreadStart, Thread> f) => f is not null;","tryCatchPattern":"try\n{\n    var s = new NewThreadScheduler(threadFactory);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"threadFactory\")\n{\n    var s = NewThreadScheduler.Default;\n}","preventionTips":["Prefer NewThreadScheduler.Default or Scheduler.NewThread over manual construction.","Centralize thread-factory creation in one factory method that guarantees non-null.","Fail fast at startup if configuration supplying the factory is missing."],"tags":["rx","scheduler","threading","argument-null","csharp"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}