应用程序未等待完成任务
Application is not waiting to finish the Task
我有 3 个方法名称(step1、step2、step3),其中一个具有密集计算。
我有这样的情况 Step1 和 Step2 相互独立,Step3 只有在 Step1
之后才能 运行
这是我的代码
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");
}
在情况 2 中,我只得到第 1 步和第 3 步的输出。
因为我已经编写了等待所有线程完成其工作的代码。所以输出应该是这样的 step1, step 3, step 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");
}
将 Int32.MaxValue
更改为 3000000,您将看到预期的结果。
我有 3 个方法名称(step1、step2、step3),其中一个具有密集计算。 我有这样的情况 Step1 和 Step2 相互独立,Step3 只有在 Step1
之后才能 运行这是我的代码
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");
}
在情况 2 中,我只得到第 1 步和第 3 步的输出。
因为我已经编写了等待所有线程完成其工作的代码。所以输出应该是这样的 step1, step 3, step 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");
}
将 Int32.MaxValue
更改为 3000000,您将看到预期的结果。