diff --git a/src/mscorlib/src/System/Threading/Tasks/FutureFactory.cs b/src/mscorlib/src/System/Threading/Tasks/FutureFactory.cs index 3f9937c3f23e..b1f634c707a3 100644 --- a/src/mscorlib/src/System/Threading/Tasks/FutureFactory.cs +++ b/src/mscorlib/src/System/Threading/Tasks/FutureFactory.cs @@ -688,15 +688,15 @@ internal static Task FromAsyncImpl( ref StackCrawlMark stackMark) { if (asyncResult == null) - throw new ArgumentNullException("asyncResult"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.asyncResult); if (endFunction == null && endAction == null) - throw new ArgumentNullException("endMethod"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.endMethod); Contract.Requires((endFunction != null) != (endAction != null), "Both endFunction and endAction were non-null"); if (scheduler == null) - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); Contract.EndContractBlock(); TaskFactory.CheckFromAsyncOptions(creationOptions, false); @@ -811,10 +811,10 @@ internal static Task FromAsyncImpl(Func FromAsyncImpl(Func FromAsyncImpl(Func FromAsyncImpl(Func; - if (promise == null) throw new ArgumentException(Environment.GetResourceString("InvalidOperation_WrongAsyncResultOrEndCalledMultiple"), "asyncResult"); + if (promise == null) ThrowHelper.ThrowArgumentException(ExceptionResource.InvalidOperation_WrongAsyncResultOrEndCalledMultiple, ExceptionArgument.asyncResult); // Grab the relevant state and then null it out so that the task doesn't hold onto the state unnecessarily var thisRef = promise.m_thisRef; var endMethod = promise.m_endMethod; promise.m_thisRef = default(TInstance); promise.m_endMethod = null; - if (endMethod == null) throw new ArgumentException(Environment.GetResourceString("InvalidOperation_WrongAsyncResultOrEndCalledMultiple"), "asyncResult"); + if (endMethod == null) ThrowHelper.ThrowArgumentException(ExceptionResource.InvalidOperation_WrongAsyncResultOrEndCalledMultiple, ExceptionArgument.asyncResult); // Complete the promise. If the IAsyncResult completed synchronously, // we'll instead complete the promise at the call site. @@ -1491,7 +1491,7 @@ private static Task CreateCanceledTask(TaskContinuationOptions continua [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func continuationFunction) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1522,7 +1522,7 @@ public Task ContinueWhenAll(Task[] tasks, Func continu [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1559,7 +1559,7 @@ public Task ContinueWhenAll(Task[] tasks, Func continu [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, TaskContinuationOptions continuationOptions) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1607,7 +1607,7 @@ public Task ContinueWhenAll(Task[] tasks, Func continu public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1634,7 +1634,7 @@ public Task ContinueWhenAll(Task[] tasks, Func continu [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1667,7 +1667,7 @@ public Task ContinueWhenAll(Task[ public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction, CancellationToken cancellationToken) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1706,7 +1706,7 @@ public Task ContinueWhenAll(Task[ public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction, TaskContinuationOptions continuationOptions) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1755,7 +1755,7 @@ public Task ContinueWhenAll(Task[ public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1771,10 +1771,10 @@ internal static Task ContinueWhenAllImpl(Task ContinueWhenAllImpl(Task[] tasks, { // check arguments TaskFactory.CheckMultiTaskContinuationOptions(continuationOptions); - if (tasks == null) throw new ArgumentNullException("tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); //ArgumentNullException of continuationFunction or continuationAction is checked by the caller Contract.Requires((continuationFunction != null) != (continuationAction != null), "Expected exactly one of endFunction/endAction to be non-null"); - if (scheduler == null) throw new ArgumentNullException("scheduler"); + if (scheduler == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); Contract.EndContractBlock(); // Check tasks array and make defensive copy @@ -1891,7 +1891,7 @@ internal static Task ContinueWhenAllImpl(Task[] tasks, [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func continuationFunction) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1922,7 +1922,7 @@ public Task ContinueWhenAny(Task[] tasks, Func continuat [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -1959,7 +1959,7 @@ public Task ContinueWhenAny(Task[] tasks, Func continuat [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, TaskContinuationOptions continuationOptions) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -2007,7 +2007,7 @@ public Task ContinueWhenAny(Task[] tasks, Func continuat public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -2034,7 +2034,7 @@ public Task ContinueWhenAny(Task[] tasks, Func continuat [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -2067,7 +2067,7 @@ public Task ContinueWhenAny(Task[ public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction, CancellationToken cancellationToken) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -2106,7 +2106,7 @@ public Task ContinueWhenAny(Task[ public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction, TaskContinuationOptions continuationOptions) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -2155,7 +2155,7 @@ public Task ContinueWhenAny(Task[ public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { - if (continuationFunction == null) throw new ArgumentNullException("continuationFunction"); + if (continuationFunction == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); Contract.EndContractBlock(); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; @@ -2170,12 +2170,12 @@ internal static Task ContinueWhenAnyImpl(Task[] tasks, { // check arguments TaskFactory.CheckMultiTaskContinuationOptions(continuationOptions); - if (tasks == null) throw new ArgumentNullException("tasks"); - if(tasks.Length == 0) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_EmptyTaskList"), "tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); + if(tasks.Length == 0) ThrowHelper.ThrowArgumentException( ExceptionResource.Task_MultiTaskContinuation_EmptyTaskList, ExceptionArgument.tasks); //ArgumentNullException of continuationFunction or continuationAction is checked by the caller Contract.Requires((continuationFunction != null) != (continuationAction != null), "Expected exactly one of endFunction/endAction to be non-null"); - if (scheduler == null) throw new ArgumentNullException("scheduler"); + if (scheduler == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); Contract.EndContractBlock(); // Call common ContinueWhenAny() setup logic, extract starter @@ -2218,11 +2218,11 @@ internal static Task ContinueWhenAnyImpl(Task Int32.MaxValue) { - throw new ArgumentOutOfRangeException("timeout"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.timeout); } return Wait((int)totalMilliseconds, default(CancellationToken)); @@ -3124,7 +3124,7 @@ public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken) { if (millisecondsTimeout < -1) { - throw new ArgumentOutOfRangeException("millisecondsTimeout"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.millisecondsTimeout); } Contract.EndContractBlock(); @@ -3879,13 +3879,13 @@ private Task ContinueWith(Action continuationAction, TaskScheduler schedul // Throw on continuation with null action if (continuationAction == null) { - throw new ArgumentNullException("continuationAction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationAction); } // Throw on continuation with null TaskScheduler if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } Contract.EndContractBlock(); @@ -4081,13 +4081,13 @@ private Task ContinueWith(Action continuationAction, Object state, // Throw on continuation with null action if (continuationAction == null) { - throw new ArgumentNullException("continuationAction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationAction); } // Throw on continuation with null TaskScheduler if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } Contract.EndContractBlock(); @@ -4296,13 +4296,13 @@ private Task ContinueWith(Func continuationFunc // Throw on continuation with null function if (continuationFunction == null) { - throw new ArgumentNullException("continuationFunction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); } // Throw on continuation with null task scheduler if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } Contract.EndContractBlock(); @@ -4515,13 +4515,13 @@ private Task ContinueWith(Func continua // Throw on continuation with null function if (continuationFunction == null) { - throw new ArgumentNullException("continuationFunction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); } // Throw on continuation with null task scheduler if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } Contract.EndContractBlock(); @@ -4573,7 +4573,7 @@ internal static void CreationOptionsFromContinuationOptions( TaskContinuationOptions illegalMask = TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning; if ((continuationOptions & illegalMask) == illegalMask) { - throw new ArgumentOutOfRangeException("continuationOptions", Environment.GetResourceString("Task_ContinueWith_ESandLR")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.continuationOptions, ExceptionResource.Task_ContinueWith_ESandLR); } // Check that no illegal options were specified @@ -4581,13 +4581,13 @@ internal static void CreationOptionsFromContinuationOptions( ~(creationOptionsMask | NotOnAnything | TaskContinuationOptions.LazyCancellation | TaskContinuationOptions.ExecuteSynchronously)) != 0) { - throw new ArgumentOutOfRangeException("continuationOptions"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.continuationOptions); } // Check that we didn't specify "not on anything" if ((continuationOptions & NotOnAnything) == NotOnAnything) { - throw new ArgumentOutOfRangeException("continuationOptions", Environment.GetResourceString("Task_ContinueWith_NotOnAnything")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.continuationOptions, ExceptionResource.Task_ContinueWith_NotOnAnything); } // This passes over all but LazyCancellation, which has no representation in TaskCreationOptions @@ -4912,7 +4912,7 @@ public static bool WaitAll(Task[] tasks, TimeSpan timeout) long totalMilliseconds = (long)timeout.TotalMilliseconds; if (totalMilliseconds < -1 || totalMilliseconds > Int32.MaxValue) { - throw new ArgumentOutOfRangeException("timeout"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.timeout); } return WaitAll(tasks, (int)totalMilliseconds); @@ -5022,11 +5022,11 @@ public static bool WaitAll(Task[] tasks, int millisecondsTimeout, CancellationTo { if (tasks == null) { - throw new ArgumentNullException("tasks"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); } if (millisecondsTimeout < -1) { - throw new ArgumentOutOfRangeException("millisecondsTimeout"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.millisecondsTimeout); } Contract.EndContractBlock(); @@ -5054,7 +5054,7 @@ public static bool WaitAll(Task[] tasks, int millisecondsTimeout, CancellationTo if (task == null) { - throw new ArgumentException(Environment.GetResourceString("Task_WaitMulti_NullTask"), "tasks"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Task_WaitMulti_NullTask, ExceptionArgument.tasks); } bool taskIsCompleted = task.IsCompleted; @@ -5135,7 +5135,7 @@ public static bool WaitAll(Task[] tasks, int millisecondsTimeout, CancellationTo // Now gather up and throw all of the exceptions. foreach (var task in tasks) AddExceptionsForCompletedTask(ref exceptions, task); Contract.Assert(exceptions != null, "Should have seen at least one exception"); - throw new AggregateException(exceptions); + ThrowHelper.ThrowAggregateException(exceptions); } return returnValue; @@ -5258,7 +5258,7 @@ internal static void FastWaitAll(Task[] tasks) // If one or more threw exceptions, aggregate them. if (exceptions != null) { - throw new AggregateException(exceptions); + ThrowHelper.ThrowAggregateException(exceptions); } } @@ -5339,7 +5339,7 @@ public static int WaitAny(Task[] tasks, TimeSpan timeout) long totalMilliseconds = (long)timeout.TotalMilliseconds; if (totalMilliseconds < -1 || totalMilliseconds > Int32.MaxValue) { - throw new ArgumentOutOfRangeException("timeout"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.timeout); } return WaitAny(tasks, (int)totalMilliseconds); @@ -5437,11 +5437,11 @@ public static int WaitAny(Task[] tasks, int millisecondsTimeout, CancellationTok { if (tasks == null) { - throw new ArgumentNullException("tasks"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); } if (millisecondsTimeout < -1) { - throw new ArgumentOutOfRangeException("millisecondsTimeout"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.millisecondsTimeout); } Contract.EndContractBlock(); @@ -5458,7 +5458,7 @@ public static int WaitAny(Task[] tasks, int millisecondsTimeout, CancellationTok if (task == null) { - throw new ArgumentException(Environment.GetResourceString("Task_WaitMulti_NullTask"), "tasks"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Task_WaitMulti_NullTask, ExceptionArgument.tasks); } if (signaledTaskIndex == -1 && task.IsCompleted) @@ -5516,7 +5516,7 @@ public static Task FromException(Exception exception) /// The faulted task. public static Task FromException(Exception exception) { - if (exception == null) throw new ArgumentNullException("exception"); + if (exception == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.exception); Contract.EndContractBlock(); var task = new Task(); @@ -5531,7 +5531,7 @@ public static Task FromException(Exception exception) public static Task FromCanceled(CancellationToken cancellationToken) { if (!cancellationToken.IsCancellationRequested) - throw new ArgumentOutOfRangeException("cancellationToken"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.cancellationToken); Contract.EndContractBlock(); return new Task(true, TaskCreationOptions.None, cancellationToken); } @@ -5543,7 +5543,7 @@ public static Task FromCanceled(CancellationToken cancellationToken) public static Task FromCanceled(CancellationToken cancellationToken) { if (!cancellationToken.IsCancellationRequested) - throw new ArgumentOutOfRangeException("cancellationToken"); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.cancellationToken); Contract.EndContractBlock(); return new Task(true, default(TResult), TaskCreationOptions.None, cancellationToken); } @@ -5554,7 +5554,7 @@ public static Task FromCanceled(CancellationToken cancellation /// The canceled task. internal static Task FromCancellation(OperationCanceledException exception) { - if (exception == null) throw new ArgumentNullException("exception"); + if (exception == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.exception); Contract.EndContractBlock(); var task = new Task(); @@ -5690,7 +5690,7 @@ public static Task Run(Func function) public static Task Run(Func function, CancellationToken cancellationToken) { // Check arguments - if (function == null) throw new ArgumentNullException("function"); + if (function == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.function); Contract.EndContractBlock(); if (AppContextSwitches.ThrowExceptionIfDisposedCancellationTokenSource) @@ -5741,7 +5741,7 @@ public static Task Run(Func> function) public static Task Run(Func> function, CancellationToken cancellationToken) { // Check arguments - if (function == null) throw new ArgumentNullException("function"); + if (function == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.function); Contract.EndContractBlock(); if (AppContextSwitches.ThrowExceptionIfDisposedCancellationTokenSource) @@ -5806,7 +5806,7 @@ public static Task Delay(TimeSpan delay, CancellationToken cancellationToken) long totalMilliseconds = (long)delay.TotalMilliseconds; if (totalMilliseconds < -1 || totalMilliseconds > Int32.MaxValue) { - throw new ArgumentOutOfRangeException("delay", Environment.GetResourceString("Task_Delay_InvalidDelay")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.delay, ExceptionResource.Task_Delay_InvalidDelay); } return Delay((int)totalMilliseconds, cancellationToken); @@ -5850,7 +5850,7 @@ public static Task Delay(int millisecondsDelay, CancellationToken cancellationTo // Throw on non-sensical time if (millisecondsDelay < -1) { - throw new ArgumentOutOfRangeException("millisecondsDelay", Environment.GetResourceString("Task_Delay_InvalidMillisecondsDelay")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.millisecondsDelay, ExceptionResource.Task_Delay_InvalidMillisecondsDelay); } Contract.EndContractBlock(); @@ -5982,18 +5982,18 @@ public static Task WhenAll(IEnumerable tasks) taskArray = new Task[taskCollection.Count]; foreach (var task in tasks) { - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); taskArray[index++] = task; } return InternalWhenAll(taskArray); } // Do some argument checking and convert tasks to a List (and later an array). - if (tasks == null) throw new ArgumentNullException("tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); List taskList = new List(); foreach (Task task in tasks) { - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); taskList.Add(task); } @@ -6031,7 +6031,7 @@ public static Task WhenAll(IEnumerable tasks) public static Task WhenAll(params Task[] tasks) { // Do some argument checking and make a defensive copy of the tasks array - if (tasks == null) throw new ArgumentNullException("tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); Contract.EndContractBlock(); int taskCount = tasks.Length; @@ -6041,7 +6041,7 @@ public static Task WhenAll(params Task[] tasks) for (int i = 0; i < taskCount; i++) { Task task = tasks[i]; - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); tasksCopy[i] = task; } @@ -6233,18 +6233,18 @@ public static Task WhenAll(IEnumerable> tasks) taskArray = new Task[taskCollection.Count]; foreach (var task in tasks) { - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); taskArray[index++] = task; } return InternalWhenAll(taskArray); } // Do some argument checking and convert tasks into a List (later an array) - if (tasks == null) throw new ArgumentNullException("tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); List> taskList = new List>(); foreach (Task task in tasks) { - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); taskList.Add(task); } @@ -6285,7 +6285,7 @@ public static Task WhenAll(IEnumerable> tasks) public static Task WhenAll(params Task[] tasks) { // Do some argument checking and make a defensive copy of the tasks array - if (tasks == null) throw new ArgumentNullException("tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); Contract.EndContractBlock(); int taskCount = tasks.Length; @@ -6295,7 +6295,7 @@ public static Task WhenAll(params Task[] tasks) for (int i = 0; i < taskCount; i++) { Task task = tasks[i]; - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); tasksCopy[i] = task; } @@ -6457,10 +6457,10 @@ internal override bool ShouldNotifyDebuggerOfWaitCompletion /// public static Task WhenAny(params Task[] tasks) { - if (tasks == null) throw new ArgumentNullException("tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); if (tasks.Length == 0) { - throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_EmptyTaskList"), "tasks"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_EmptyTaskList, ExceptionArgument.tasks); } Contract.EndContractBlock(); @@ -6471,7 +6471,7 @@ public static Task WhenAny(params Task[] tasks) for (int i = 0; i < taskCount; i++) { Task task = tasks[i]; - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); tasksCopy[i] = task; } @@ -6496,7 +6496,7 @@ public static Task WhenAny(params Task[] tasks) /// public static Task WhenAny(IEnumerable tasks) { - if (tasks == null) throw new ArgumentNullException("tasks"); + if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks); Contract.EndContractBlock(); // Make a defensive copy, as the user may manipulate the tasks collection @@ -6504,13 +6504,13 @@ public static Task WhenAny(IEnumerable tasks) List taskList = new List(); foreach (Task task in tasks) { - if (task == null) throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_NullTask"), "tasks"); + if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks); taskList.Add(task); } if (taskList.Count == 0) { - throw new ArgumentException(Environment.GetResourceString("Task_MultiTaskContinuation_EmptyTaskList"), "tasks"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_EmptyTaskList, ExceptionArgument.tasks); } // Previously implemented CommonCWAnyLogic() can handle the rest diff --git a/src/mscorlib/src/System/Threading/Tasks/TaskCompletionSource.cs b/src/mscorlib/src/System/Threading/Tasks/TaskCompletionSource.cs index 6ff70eb79c27..8b1dd2a62f75 100644 --- a/src/mscorlib/src/System/Threading/Tasks/TaskCompletionSource.cs +++ b/src/mscorlib/src/System/Threading/Tasks/TaskCompletionSource.cs @@ -156,7 +156,7 @@ private void SpinUntilCompleted() /// The was disposed. public bool TrySetException(Exception exception) { - if (exception == null) throw new ArgumentNullException("exception"); + if (exception == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.exception); bool rval = m_task.TrySetException(exception); if (!rval && !m_task.IsCompleted) SpinUntilCompleted(); @@ -185,18 +185,18 @@ public bool TrySetException(Exception exception) /// The was disposed. public bool TrySetException(IEnumerable exceptions) { - if (exceptions == null) throw new ArgumentNullException("exceptions"); + if (exceptions == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.exceptions); List defensiveCopy = new List(); foreach (Exception e in exceptions) { if (e == null) - throw new ArgumentException(Environment.GetResourceString("TaskCompletionSourceT_TrySetException_NullException"), "exceptions"); + ThrowHelper.ThrowArgumentException(ExceptionResource.TaskCompletionSourceT_TrySetException_NullException, ExceptionArgument.exceptions); defensiveCopy.Add(e); } if (defensiveCopy.Count == 0) - throw new ArgumentException(Environment.GetResourceString("TaskCompletionSourceT_TrySetException_NoExceptions"), "exceptions"); + ThrowHelper.ThrowArgumentException(ExceptionResource.TaskCompletionSourceT_TrySetException_NoExceptions, ExceptionArgument.exceptions); bool rval = m_task.TrySetException(defensiveCopy); if (!rval && !m_task.IsCompleted) SpinUntilCompleted(); @@ -238,11 +238,11 @@ internal bool TrySetException(IEnumerable exceptions) /// The was disposed. public void SetException(Exception exception) { - if (exception == null) throw new ArgumentNullException("exception"); + if (exception == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.exception); if (!TrySetException(exception)) { - throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted")); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted); } } @@ -268,7 +268,7 @@ public void SetException(IEnumerable exceptions) { if (!TrySetException(exceptions)) { - throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted")); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted); } } @@ -316,7 +316,7 @@ public bool TrySetResult(TResult result) public void SetResult(TResult result) { if (!TrySetResult(result)) - throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted")); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted); } /// @@ -364,7 +364,7 @@ public bool TrySetCanceled(CancellationToken cancellationToken) public void SetCanceled() { if(!TrySetCanceled()) - throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted")); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted); } } } diff --git a/src/mscorlib/src/System/Threading/Tasks/future.cs b/src/mscorlib/src/System/Threading/Tasks/future.cs index 9ce7ab60ebb1..39e6ca1d45f9 100644 --- a/src/mscorlib/src/System/Threading/Tasks/future.cs +++ b/src/mscorlib/src/System/Threading/Tasks/future.cs @@ -350,7 +350,7 @@ internal Task(Func valueSelector, Task parent, CancellationToken cancel { if ((internalOptions & InternalTaskOptions.SelfReplicating) != 0) { - throw new ArgumentOutOfRangeException("creationOptions", Environment.GetResourceString("TaskT_ctor_SelfReplicating")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions, ExceptionResource.TaskT_ctor_SelfReplicating); } } @@ -380,7 +380,7 @@ internal Task(Delegate valueSelector, object state, Task parent, CancellationTok { if ((internalOptions & InternalTaskOptions.SelfReplicating) != 0) { - throw new ArgumentOutOfRangeException("creationOptions", Environment.GetResourceString("TaskT_ctor_SelfReplicating")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions, ExceptionResource.TaskT_ctor_SelfReplicating); } } @@ -391,15 +391,15 @@ internal static Task StartNew(Task parent, Func function, Canc { if (function == null) { - throw new ArgumentNullException("function"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.function); } if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } if ((internalOptions & InternalTaskOptions.SelfReplicating) != 0) { - throw new ArgumentOutOfRangeException("creationOptions", Environment.GetResourceString("TaskT_ctor_SelfReplicating")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions, ExceptionResource.TaskT_ctor_SelfReplicating); } // Create and schedule the future. @@ -415,15 +415,15 @@ internal static Task StartNew(Task parent, Func functi { if (function == null) { - throw new ArgumentNullException("function"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.function); } if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } if ((internalOptions & InternalTaskOptions.SelfReplicating) != 0) { - throw new ArgumentOutOfRangeException("creationOptions", Environment.GetResourceString("TaskT_ctor_SelfReplicating")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions, ExceptionResource.TaskT_ctor_SelfReplicating); } // Create and schedule the future. @@ -879,12 +879,12 @@ internal Task ContinueWith(Action> continuationAction, TaskSchedul { if (continuationAction == null) { - throw new ArgumentNullException("continuationAction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationAction); } if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } TaskCreationOptions creationOptions; @@ -1083,12 +1083,12 @@ internal Task ContinueWith(Action, Object> continuationAction, Obj { if (continuationAction == null) { - throw new ArgumentNullException("continuationAction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationAction); } if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } TaskCreationOptions creationOptions; @@ -1310,12 +1310,12 @@ internal Task ContinueWith(Func, TNewResul { if (continuationFunction == null) { - throw new ArgumentNullException("continuationFunction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); } if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } TaskCreationOptions creationOptions; @@ -1544,12 +1544,12 @@ internal Task ContinueWith(Func, Object, T { if (continuationFunction == null) { - throw new ArgumentNullException("continuationFunction"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.continuationFunction); } if (scheduler == null) { - throw new ArgumentNullException("scheduler"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler); } TaskCreationOptions creationOptions; @@ -1594,7 +1594,7 @@ ref stackMark IDisposable IObservable.Subscribe(IObserver observer) { if (observer == null) - throw new System.ArgumentNullException("observer"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.observer); var continuationTask = diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs index db1419d2cef1..3105d56f7c3d 100644 --- a/src/mscorlib/src/System/ThrowHelper.cs +++ b/src/mscorlib/src/System/ThrowHelper.cs @@ -36,7 +36,8 @@ namespace System { // multiple times for different instantiation. // - using System.Runtime.CompilerServices; + using Collections.Generic; + using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Diagnostics.Contracts; @@ -129,11 +130,21 @@ internal static void ThrowObjectDisposedException(string objectName, ExceptionRe throw new ObjectDisposedException(objectName, Environment.GetResourceString(GetResourceName(resource))); } + internal static void ThrowObjectDisposedException(ExceptionResource resource) + { + throw new ObjectDisposedException(null, Environment.GetResourceString(GetResourceName(resource))); + } + internal static void ThrowNotSupportedException() { throw new NotSupportedException(); } + internal static void ThrowAggregateException(List exceptions) + { + throw new AggregateException(exceptions); + } + // Allow nulls for reference types and Nullable, but not for value types. // Aggressively inline so the jit evaluates the if in place and either drops the call altogether // Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException @@ -218,6 +229,24 @@ internal enum ExceptionArgument { comparer, endIndex, keys, + creationOptions, + timeout, + tasks, + scheduler, + continuationFunction, + millisecondsTimeout, + millisecondsDelay, + function, + exceptions, + exception, + cancellationToken, + delay, + asyncResult, + endMethod, + endFunction, + beginMethod, + continuationOptions, + continuationAction, } @@ -289,6 +318,29 @@ internal enum ExceptionResource { ArgumentOutOfRange_EndIndexStartIndex, Arg_LowerBoundsMustMatch, Arg_BogusIComparer, + Task_WaitMulti_NullTask, + Task_ThrowIfDisposed, + Task_Start_TaskCompleted, + Task_Start_Promise, + Task_Start_ContinuationTask, + Task_Start_AlreadyStarted, + Task_RunSynchronously_TaskCompleted, + Task_RunSynchronously_Continuation, + Task_RunSynchronously_Promise, + Task_RunSynchronously_AlreadyStarted, + Task_MultiTaskContinuation_NullTask, + Task_MultiTaskContinuation_EmptyTaskList, + Task_Dispose_NotCompleted, + Task_Delay_InvalidMillisecondsDelay, + Task_Delay_InvalidDelay, + Task_ctor_LRandSR, + Task_ContinueWith_NotOnAnything, + Task_ContinueWith_ESandLR, + TaskT_TransitionToFinal_AlreadyCompleted, + TaskT_ctor_SelfReplicating, + TaskCompletionSourceT_TrySetException_NullException, + TaskCompletionSourceT_TrySetException_NoExceptions, + InvalidOperation_WrongAsyncResultOrEndCalledMultiple, } }