当前位置: 首页>>代码示例>>C#>>正文


C# Task.ToList方法代码示例

本文整理汇总了C#中Task.ToList方法的典型用法代码示例。如果您正苦于以下问题:C# Task.ToList方法的具体用法?C# Task.ToList怎么用?C# Task.ToList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Task的用法示例。


在下文中一共展示了Task.ToList方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        public void Run()
        {
            var tasks = new Task<int>[3];

            tasks[0] = Task.Run(() =>
            {
                Thread.Sleep(2000);
                return 1;
            });
            tasks[1] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                return 2;
            });
            tasks[2] = Task.Run(() =>
            {
                Thread.Sleep(3000);
                return 3;
            });

            while (tasks.Length > 0)
            {
                int i = Task.WaitAny(tasks);
                Task<int> completedTask = tasks[i];

                Console.WriteLine(completedTask.Result);

                List<Task<int>> temp = tasks.ToList();
                temp.RemoveAt(i);
                tasks = temp.ToArray();
            }
        }
开发者ID:rrsc,项目名称:ProgrammingInCSharp,代码行数:32,代码来源:UsingTaskWaitAny.cs

示例2: TestFastBatchEnqueueDequeue

        public void TestFastBatchEnqueueDequeue()
        {
            int count = 1000;

            var source = new string[count * 3];

            for (var i = 0; i < source.Length; i++)
            {
                source[i] = Guid.NewGuid().ToString();
            }

            IBatchQueue<string> queue = new ConcurrentBatchQueue<string>(count * 3);

            Task[] tasks = new Task[count];

            for (var i = 0; i < count; i++)
            {
                tasks[i] = new Task((j) =>
                {
                    var index = (int)j;
                    queue.Enqueue(new string[] { source[index * 3], source[index * 3 + 1], source[index * 3 + 2] });
                }, i);
            }

            tasks.ToList().ForEach(t => t.Start());

            var outputList = new List<string>();

            var spinWait = new SpinWait();

            while (outputList.Count < source.Length)
            {
                if (!queue.TryDequeue(outputList))
                    spinWait.SpinOnce();
                else
                    Console.WriteLine("Count:" + outputList.Count);
            }

            var dict = source.ToDictionary(s => s);

            for (var i = 0; i < outputList.Count; i++)
            {
                var line = outputList[i];

                if (!dict.Remove(line))
                    Assert.Fail("Dequeue error");
            }

            Assert.AreEqual(0, dict.Count);
        }
开发者ID:xalexchen,项目名称:SuperSocket,代码行数:50,代码来源:BatchQueueTest.cs

示例3: Main

 static void Main(string[] args)
 {
     Task<int>[] tasks = new Task<int>[3];
     tasks[0] = Task.Run(() => { Thread.Sleep(2000); return 1; });
     tasks[1] = Task.Run(() => { Thread.Sleep(1000); return 2; });
     tasks[2] = Task.Run(() => { Thread.Sleep(3000); return 3; });
     while (tasks.Length > 0)
     {
         int i = Task.WaitAny(tasks);
         Task<int> completedTask = tasks[i];
         Console.WriteLine(completedTask.Result);
         var temp = tasks.ToList();
         temp.RemoveAt(i);
         tasks = temp.ToArray();
     }
     Console.ReadLine();
 }
开发者ID:,项目名称:,代码行数:17,代码来源:

示例4: WaitAny

		public int WaitAny ()
		{

			Task<int>[] tasks = new Task<int>[3];

			tasks [0] = Task.Run (() => {
				Thread.Sleep (10);
				return 1;
			});

			tasks [1] = Task.Run (() => {
				Thread.Sleep (20);
				return 2;
			});

			tasks [2] = Task.Run (() => {
				Thread.Sleep (30);
				return 3;
			});

			int result = 0;

			while (result < 6) { 
				int i = Task.WaitAny (tasks);
				var completedTask = tasks [i];                 

				result += completedTask.Result;

				var taskList = tasks.ToList ();
				taskList.RemoveAt (i);

				tasks = taskList.ToArray ();
			} 

			return result;
		}
开发者ID:caloggins,项目名称:DOT-NET-on-Linux,代码行数:36,代码来源:TaskExample.cs

示例5: Main

        static void Main(string[] args)
        {
            Task<Int32[]> parent = Task.Run(() =>
            {
                var result = new Int32[3];
                TaskFactory tf = new TaskFactory(TaskCreationOptions.AttachedToParent, TaskContinuationOptions.ExecuteSynchronously);

                tf.StartNew(() => result[0] = 0);
                tf.StartNew(() => result[1] = 1);
                tf.StartNew(() => result[2] = 2);
                return result;
            });

            var finalTask = parent.ContinueWith(parentTask =>
            {
                foreach (int item in parent.Result)
                {
                    Console.WriteLine(item);
                }
            });

            finalTask.Wait();
            Console.Read();
            Console.WriteLine("----------------------------------------------------------------------------------\n\n\n");

            Task[] tasks = new Task[3];
            tasks[0] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("1");
                return 1;
            });
            tasks[1] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("2");
                return 2;
            });
            tasks[2] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("3");
                return 3;
            }
            );
            Task.WaitAll(tasks);
            Console.Read();
            Console.ReadLine();

            Console.WriteLine("----------------------------------------------------------------------------------\n\n\n");
            Console.ReadLine();

            Task<int>[] taskss = new Task<int>[3];
            taskss[0] = Task.Run(() => { Thread.Sleep(2000); return 1; });
            taskss[1] = Task.Run(() => { Thread.Sleep(1000); return 2; });
            taskss[2] = Task.Run(() => { Thread.Sleep(3000); return 3; });
            while (tasks.Length > 0)
            {
                int i = Task.WaitAny(tasks);
                Task<int> completedTask = taskss[i];
                Console.WriteLine(completedTask.Result);
                var temp = taskss.ToList();
                temp.RemoveAt(i);
                taskss = temp.ToArray();
            }
            Console.Read();
            Console.ReadLine();
        }
开发者ID:ErhanGDC,项目名称:MyWorks,代码行数:68,代码来源:Program.cs

示例6: Main


//.........这里部分代码省略.........
                var results = new Int32[3];

                TaskFactory tf = new TaskFactory(TaskCreationOptions.AttachedToParent, TaskContinuationOptions.ExecuteSynchronously);

                tf.StartNew(() => results[0] = 0);
                tf.StartNew(() => results[1] = 1);
                tf.StartNew(() => results[2] = 2);

                return results;

            });

            var finalTask1 = parent.ContinueWith(parentTask =>
            {
                foreach (var i in parentTask.Result)
                    Console.WriteLine(i);
            });

            finalTask.Wait(); //FINAL TASK WILL ONLY RUN ONCE THE PARENT TASK HAS FINISHED

            Console.WriteLine("  ");
            Console.WriteLine("1-14");
            Console.WriteLine("  ");

            //1-14 In this case all tasks are exeuted simultaneously

            Task<int>[] tasks = new Task<int>[3];

            tasks[0] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("Task 1");
                return 1;
            });

            tasks[1] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("Task 2");
                return 2;
            });

            tasks[2] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("Task 3");
                return 3;
            });

            Task.WaitAll(tasks); //will wait until all tasks have finished.

            //Task.WhenAll(tasks); use this to specify continuation method

            //Task.WaitAny(tasks); use this to wait until any of the tasks has finished - example below.

            Console.WriteLine("  ");
            Console.WriteLine("1-15");
            Console.WriteLine("  ");

            //1-15 In this example we process a completed Task as soon as it finished.
            //Finished tasks are processed in the while loop and then removed from the array.
            //By keeping track of which tasks are finished we don't neet wait until all tasks have completed.

            tasks = new Task<int>[3];

            tasks[0] = Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("Task 1");
                return 1;
            });

            tasks[1] = Task.Run(() =>
            {
                Thread.Sleep(3000);
                Console.WriteLine("Task 2");
                return 2;
            });

            tasks[2] = Task.Run(() =>
            {
                Thread.Sleep(2000);
                Console.WriteLine("Task 3");
                return 3;
            });

            while(tasks.Length >0)
            {
                int i = Task.WaitAny(tasks);
                Task<int> completedTask = tasks[i];
                Console.WriteLine(completedTask.Result);

                var temp = tasks.ToList();
                temp.RemoveAt(i);
                tasks = temp.ToArray();

            }

            Console.ReadKey();
        }
开发者ID:damian996,项目名称:70-483,代码行数:101,代码来源:Program.cs


注:本文中的Task.ToList方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。