當前位置: 首頁>>代碼示例>>C#>>正文


C# Task.ForEach方法代碼示例

本文整理匯總了C#中System.Threading.Tasks.Task.ForEach方法的典型用法代碼示例。如果您正苦於以下問題:C# Task.ForEach方法的具體用法?C# Task.ForEach怎麽用?C# Task.ForEach使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Threading.Tasks.Task的用法示例。


在下文中一共展示了Task.ForEach方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: TaskFromPromiseWithResultIndexWorksWhenPromiseCompletes

		public void TaskFromPromiseWithResultIndexWorksWhenPromiseCompletes() {
			var promise = CreatePromise();
			var tasks = new Task<int>[] {
				Task.FromPromise<int>(promise, 0),
				Task.FromPromise<int>(promise, 1),
				Task.FromPromise<int>(promise, 2),
				Task.FromPromise<int>(promise, -3),
				Task.FromPromise<int>(promise, -2),
				Task.FromPromise<int>(promise, -1),
			};
			var continuationsRun = new bool[6];
			Assert.IsTrue(tasks.Every(t => t.Status == TaskStatus.Running), "Tasks should be running after being created");
			tasks.ForEach((x, i, _) => x.ContinueWith(t => {
				Assert.IsTrue(t == x, "ContinueWith parameter should be correct");
				continuationsRun[i] = true;
			}));

			Window.SetTimeout(() => {
				Assert.IsFalse(continuationsRun.Some(x => x), "Continuations should not be run too early.");
				Assert.IsTrue(tasks.Every(t => t.Status == TaskStatus.Running), "Tasks should be running before promise is completed.");
				promise.Resolve(10, 42, 38);
			}, 100);

			Window.SetTimeout(() => {
				Assert.IsTrue(tasks.Every(t => t.Status == TaskStatus.RanToCompletion), "Task should be completed after promise");
				Assert.IsTrue(continuationsRun.Every(x => x), "Continuations should have been run after promise was completed.");
				Assert.AreEqual(tasks[0].Result, 10, "Task 0 result should be correct");
				Assert.AreEqual(tasks[1].Result, 42, "Task 1 result should be correct");
				Assert.AreEqual(tasks[2].Result, 38, "Task 2 result should be correct");
				Assert.AreEqual(tasks[3].Result, 10, "Task 3 result should be correct");
				Assert.AreEqual(tasks[4].Result, 42, "Task 4 result should be correct");
				Assert.AreEqual(tasks[5].Result, 38, "Task 5 result should be correct");
				QUnit.Start();
			}, 200);
		}
開發者ID:JimmyJune,項目名稱:SaltarelleCompiler,代碼行數:35,代碼來源:PromiseTests.cs

示例2: SpinParallel

        /// <summary>
        ///     Runs given function in parallel on multiple tasks.
        ///     Number of tasks spawned matches number of logical processors.
        /// </summary>
        /// <param name="millisecondsToRun"></param>
        /// <param name="funcToTest"></param>
        /// <returns></returns>
        public static long SpinParallel(long millisecondsToRun, Action funcToTest)
        {
            Task<long>[] tasks = new Task<long>[Environment.ProcessorCount];

            for(int i = 0; i < Environment.ProcessorCount; i++)
            {
                tasks[i] = new Task<long>(() => Spin(millisecondsToRun, funcToTest));
            }

            DateTime start = DateTime.UtcNow;

            tasks.ForEach(task => task.Start());
            // ReSharper disable once CoVariantArrayConversion
            Task.WaitAll(tasks);

            long ranMillisec = (long)(DateTime.UtcNow - start).TotalMilliseconds;
            Debug.WriteLine("Parallel call counting run time mismatch is {0:#,#} milliseconds.", ranMillisec - millisecondsToRun);

            long[] runCounts = tasks.Select(thread => thread.Result).ToArray();
            long total = runCounts.Sum();
            return total;
        }
開發者ID:vgribok,項目名稱:Aspectacular,代碼行數:29,代碼來源:RunCounter.cs


注:本文中的System.Threading.Tasks.Task.ForEach方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。