在 C# 中等待线程完成Task.WaitAll和Thread.Join

作者: zengde 分类: 笔记 发布时间: 2023-08-22 07:02

1.Task.WaitAll()等待线程以 C#方法结束
2.Thread.Join()等待线程以 C#方法结束

Task.WaitAll()等待线程以C#方法结束
C#Task.WaitAll()方法用于Task等待类中所有对象的完成。Task类代表 C# 异步任务。Task您可以在您的类中启动一个线程并等待 C#Task.WaitAll()方法完成该线程。

using System;
using System.Threading.Tasks;
 
namespace wait_for_thread
{
    class Program
    {
        static void fun1()
        {
            for(int i = 0; i < 2; i++)
            {
                Console.WriteLine("Thread 1");
            }
        }
        static void fun2()
        {
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Thread 2");
            }
        }
        static void Main(string[] args)
        {
            Task thread1 = Task.Factory.StartNew(() => fun1());
            Task thread2 = Task.Factory.StartNew(() => fun2());
            Task.WaitAll(thread1, thread2);
            Console.WriteLine("The End");
        }
    }
}

输出:

Thread 1
Thread 1
Thread 2
Thread 2
The End

在上面的代码中,我使用了 C# 中的一个方法来等待Task.WaitAll()主线程中的任务thread1thread2任务完成。

using System.Threading.Tasks;
using System.Threading;
 
class Program
{
    static void Main(string[] args)
    {
        Task task1 = Task.Factory.StartNew(() => doStuff("Task1"));
        Task task2 = Task.Factory.StartNew(() => doStuff("Task2"));
        Task task3 = Task.Factory.StartNew(() => doStuff("Task3"));
        Task.WaitAll(task1, task2, task3);
 
        Console.WriteLine("All threads complete");
        Console.ReadLine();
    }
 
    static void doStuff(string strName)
    {
        for (int i = 1; i <= 3; i++)
        {
            Console.WriteLine(strName + " " + i.ToString());
            Thread.Yield();
        }
    }
}
Task3 1
Task1 1
Task3 2
Task1 2
Task2 1
Task1 3
Task3 3
Task2 2
Task2 3
All threads complete

Thread.Join()等待线程以C#方法结束

上面的部分Task.WaitAll()描述了如何使用 C# 方法来等待一个线程。您可以使用C#Thread.Join()方法实现相同的目标。Thread.Join()该方法停止调用线程的执行,直到当前线程完成执行。下面的代码示例展示了如何使用 C#Thread.Join()方法等待线程完成执行。

using System;
using System.Threading.Tasks;
 
namespace wait_for_thread
{
    class Program
    {
        static void fun1()
        {
            for(int i = 0; i < 2; i++)
            {
                Console.WriteLine("Thread 1");
            }
        }
        static void fun2()
        {
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Thread 2");
            }
        }
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(new ThreadStart(fun1));
            Thread thread2 = new Thread(new ThreadStart(fun2));
            
            thread1.Start();
            thread1.Join();
            thread2.Start();
            thread2.Join();
            Console.WriteLine("The End");
        }
    }
}

输出:

Thread 1
Thread 1
Thread 2
Thread 2
The End

在上面的代码中,我使用了一个 C#Thread.Join()方法来等待thread1线程和thread2线程在主线程内完成。

using System.Threading.Tasks;
using System.Threading;
 
class Program
{
    static void Main(string[] args)
    {
        Thread t1 = new Thread(doStuff);
        t1.Start("Task1");
        Thread t2 = new Thread(doStuff);
        t2.Start("Task2");
        Thread t3 = new Thread(doStuff);
        t3.Start("Task3");
        t1.Join(); //wait here until t1 has terminated...
        t2.Join(); //wait here until t2 has terminated...
        t3.Join(); //wait here until t3 has terminated...
        Console.WriteLine("All threads complete");
    }
    static void doStuff(object strName)
    {
        for (int i = 1; i <= 3; i++)
        {
            Console.WriteLine(strName + " " + i.ToString());
            Thread.Yield();
        }
    }
}
Task1 1
Task1 2
Task1 3
Task2 1
Task2 2
Task2 3
Task3 1
Task3 2
Task3 3
All threads complete