C# 应用程序未等待完成任务
我有3个方法名称(step1、step2、step3),其中一个有密集计算。 我有这样的情况,如步骤1和步骤2是相互独立的,步骤3只能在步骤1之后运行 这是我的密码C# 应用程序未等待完成任务,c#,task,continuations,C#,Task,Continuations,我有3个方法名称(step1、step2、step3),其中一个有密集计算。 我有这样的情况,如步骤1和步骤2是相互独立的,步骤3只能在步骤1之后运行 这是我的密码 static void Main(string[] args) { Console.WriteLine("Step1, Step2and Step3are independent of each other.\n"); Console.WriteLine("Step1 and Step2 are independe
static void Main(string[] args)
{
Console.WriteLine("Step1, Step2and Step3are independent of each other.\n");
Console.WriteLine("Step1 and Step2 are independent of each other, and Step3 can be run only after Step1\n \n");
Console.WriteLine("Step1 and Step2 are independent of each other, and Step3 can be run only after Step1 and Step2 finish.\n \n");
Console.WriteLine(" Step1 and Step2 are independent of each other, and Step3 can be run only after Step1 or Step2 finishes.\n \n");
var getCase = Int32.Parse(Console.ReadLine());
switch (getCase)
{
case 1:
Parallel.Invoke(Step1, Step2, Step3);
break;
case 2:
Task taskStep1 = Task.Run(() => Step1());
Task taskStep2 = Task.Run(() => Step2());
Task taskStep3 = taskStep1.ContinueWith((previousTask) => Step3());
Task.WaitAll(taskStep2, taskStep3);
break;
case 3:
Task step1Task = Task.Run(() => Step1());
Task step2Task = Task.Run(() => Step2());
Task step3Task = Task.Factory.ContinueWhenAll(
new Task[] { step1Task, step2Task },
(previousTasks) => Step3());
step3Task.Wait();
break;
case 4:
Task TaskStep1 = Task.Run(() => Step1());
Task Taskstep2 = Task.Run(() => Step2());
Task Taskstep3 = Task.Factory.ContinueWhenAny(
new Task[] { TaskStep1, Taskstep2 },
(previousTask) => Step3());
Taskstep3.Wait();
break;
}
Console.ReadLine();
}
static void Step1()
{
Console.WriteLine("Step1");
}
static void Step2()
{
double result = 10000000d;
var maxValue = Int32.MaxValue;
for (int i = 1; i < maxValue; i++)
{
result /= i;
}
Console.WriteLine("Step2");
}
static void Step3()
{
Console.WriteLine("Step3");
}
static void Main(字符串[]args)
{
WriteLine(“步骤1、步骤2和步骤3相互独立。\n”);
Console.WriteLine(“Step1和Step2相互独立,Step3只能在Step1之后运行\n\n”);
Console.WriteLine(“步骤1和步骤2相互独立,只有在步骤1和步骤2完成后才能运行步骤3。\n\n”);
Console.WriteLine(“步骤1和步骤2相互独立,步骤3只能在步骤1或步骤2完成后运行。\n\n”);
var getCase=Int32.Parse(Console.ReadLine());
开关(getCase)
{
案例1:
并行调用(步骤1、步骤2、步骤3);
打破
案例2:
Task taskStep1=Task.Run(()=>Step1());
Task taskStep2=Task.Run(()=>Step2());
Task taskStep3=taskStep1.继续((上一个Task)=>Step3());
Task.WaitAll(taskStep2,taskStep3);
打破
案例3:
Task step1Task=Task.Run(()=>Step1());
Task step2Task=Task.Run(()=>Step2());
任务步骤3任务=Task.Factory.continuewhall(
新任务[]{step1Task,step2Task},
(以前的任务)=>步骤3());
第三步任务。等待();
打破
案例4:
Task TaskStep1=Task.Run(()=>Step1());
Task Taskstep2=Task.Run(()=>Step2());
Task Taskstep3=Task.Factory.ContinueWhenAny(
新任务[]{TaskStep1,Taskstep2},
(上一个任务)=>步骤3();
任务步骤3.等待();
打破
}
Console.ReadLine();
}
静态无效步骤1()
{
控制台写入线(“步骤1”);
}
静态无效步骤2()
{
双结果=10000000d;
var maxValue=Int32.maxValue;
对于(int i=1;i
在案例2中,我只得到了第1步、第3步的输出
正如我所写的代码,等待所有线程完成它们的工作。所以输出应该是这样的步骤1、步骤3、步骤2我已经测试了您的代码,它工作得很好,问题是从1迭代到
Int32。MaxValue
需要很长时间(在我的计算机上大约15秒),在以下代码中:
static void Step2()
{
double result = 10000000d;
var maxValue = Int32.MaxValue;
for (int i = 1; i < maxValue; i++)
{
result /= i;
}
Console.WriteLine("Step2");
}
静态无效步骤2()
{
双结果=10000000d;
var maxValue=Int32.maxValue;
对于(int i=1;i
将
Int32.MaxValue
更改为3000000,您将看到预期结果。在LINQPad中运行此操作,我无法重现您的问题。我得到了预期的输出。我同意,在案例2中似乎没有问题。只需等待几秒钟,Step2
将完成。只有在Step2
之后,任务.WaitAll
才能按计划完成。