简体   繁体   English

如何在C#中的第一个和第二个方法之后执行第三个方法

[英]how to execute a third method after first and second method in c#

I have two methods running in threads by using Task class. 我通过使用Task类在线程中运行了两种方法。 I have a third method which is executing in main thread. 我有第三种方法正在主线程中执行。 I want third method to be executed after first and second method. 我希望在第一个和第二个方法之后执行第三个方法。 How to do this in following code. 在下面的代码中如何做到这一点。 After Firstmethod() and Secondmethod() only Thirdmethod() to be executed Firstmethod()Secondmethod()Thirdmethod()将被执行

static void Main(string[] args)
{
    Task.Factory.StartNew(() => { Firstmethod();
    });
    Task.Factory.StartNew(() => { Secondmethod();
    });

        Thirdmethod();
    Console.ReadLine();
}

static void Firstmethod()
{
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(i);
    }
}
static void Secondmethod()
{
    for (int i = 10; i < 20; i++)
    {
        Console.WriteLine(i);
    }
}
static void Thirdmethod()
{
    for (int i = 20; i < 30; i++)
    {
        Console.WriteLine(i);
    }
}

Use Task.WaitAll . 使用Task.WaitAll It's available in .NET 4.0. 在.NET 4.0中可用。

static void Main(string[] args)
{
    Task t1 = Task.Factory.StartNew(() => {
        Firstmethod();
    });
    Task t2 = Task.Factory.StartNew(() => {
        Secondmethod();
    });

    Task.WaitAll(t1, t2);
    Thirdmethod();
    Console.ReadLine();
}

While Jakub's answer is correct, it could be more efficient. 尽管雅库布(Jakub)的答案是正确的,但它可能更有效。 Using Task.WaitAll blocks the thread while 2 other threads perform the first and second operations. 使用Task.WaitAll阻止线程,而其他2个线程执行第一个和第二个操作。

Instead of blocking that thread you can use it to execute one of the methods, and only then block on the other one. 不用阻塞该线程,您可以使用它执行一种方法,然后仅阻塞另一种方法。 This will only use 2 threads instead of 3 and may even not block at all: 这将仅使用2个线程而不是3个线程,甚至可能根本不会阻塞:

static void Main()
{
    Task task = Task.Factory.StartNew(() => FirstMethod()); // use another thread
    SecondMethod(); // use the current thread
    task.Wait(); // make sure the first method completed
    Thirdmethod();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 第一种方法抛出异常后如何恢复第二种方法 C# - How to resume second method after first method throws an exception C# 首先完成后如何在线程的同一实例上执行第二种方法 - how to execute second method on same instance of thread after finishing first 在C#中,如何从第一类调用第二类中的方法? - In C# how to call a method in a second class from the first class? 在第一个方法完成线程功能后启动第二个方法 c# - Start second method after first method is finished with thread functionality c# C#如何仅显示组合的第一个,第二个和第三个答案? - C# How to display the first, second and third answer only of the combination? 如何在MVC C#中继续第二个方法之前等待第一个异步await方法? - How to wait for first async await method before continuing second method in mvc c#? 如何在C#中执行服务器应用程序方法 - how to execute server application method in C# 如何使用C#ScriptEngine在类中执行方法 - How to execute a method in class with C# ScriptEngine C#异步方法基于第一个完成的任务执行 - C# async method execute base on the first completed task 如何通过Func发送一个方法来执行C#中的另一个方法? - How to send a method to execute to another method in C# via Func?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM