本文整理汇总了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();
}
}
示例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);
}
示例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();
}
示例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;
}
示例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();
}
示例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();
}