在C#中,多线程编程主要涉及使用System.Threading命名空间下的类和接口来创建和管理线程。以下是一些C#多线程编程的基本用法和示例:
1. 使用Thread类创建线程
 
csharp代码 
| using System; | |
| using System.Threading; | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| // 创建一个新的线程 | |
| Thread newThread = new Thread(new ThreadStart(ThreadFunction)); | |
| // 启动线程 | |
| newThread.Start(); | |
| // 等待线程完成 | |
| newThread.Join(); | |
| Console.WriteLine("Thread completed."); | |
| } | |
| static void ThreadFunction() | |
| { | |
| Console.WriteLine("Hello from a new thread!"); | |
| } | |
| } | 
2. 使用Task和Task<T>类进行异步编程
 
Task类提供了基于任务的异步编程模型,它是推荐的方式来进行多线程编程,因为它提供了更好的控制和简洁的语法。
csharp代码 
| using System; | |
| using System.Threading.Tasks; | |
| class Program | |
| { | |
| static async Task Main() | |
| { | |
| Console.WriteLine("Starting a task..."); | |
| // 启动一个任务 | |
| Task task = Task.Run(() => | |
| { | |
| Console.WriteLine("Hello from a task!"); | |
| }); | |
| // 等待任务完成 | |
| await task; | |
| Console.WriteLine("Task completed."); | |
| } | |
| } | 
3. 使用Parallel类进行并行编程
 
Parallel类提供了并行执行循环和操作的功能。
csharp代码 
| using System; | |
| using System.Threading.Tasks; | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| // 使用Parallel.For并行执行循环 | |
| Parallel.For(0, 10, i => | |
| { | |
| Console.WriteLine($"Processing {i} on thread {Thread.CurrentThread.ManagedThreadId}"); | |
| }); | |
| } | |
| } | 
4. 使用async和await关键字进行异步编程
 
async和await关键字使得异步编程更加简单和直观。
csharp代码 
| using System; | |
| using System.Threading.Tasks; | |
| class Program | |
| { | |
| static async Task Main() | |
| { | |
| Console.WriteLine("Starting an asynchronous operation..."); | |
| // 调用一个异步方法 | |
| int result = await PerformAsyncOperation(); | |
| Console.WriteLine($"Operation completed with result: {result}"); | |
| } | |
| static async Task<int> PerformAsyncOperation() | |
| { | |
| // 模拟一个异步操作 | |
| await Task.Delay(1000); | |
| return 42; // 返回结果 | |
| } | |
| } | 
5. 使用ThreadPool
 
线程池(ThreadPool)是.NET Framework提供的一个线程管理机制,它允许你请求一个线程来执行一些任务,而不需要自己创建和管理线程。
csharp代码 
| using System; | |
| using System.Threading; | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| // 将任务排入线程池队列 | |
| ThreadPool.QueueUserWorkItem(o => | |
| { | |
| Console.WriteLine("Hello from the thread pool!"); | |
| }); | |
| Console.WriteLine("Task queued to the thread pool."); | |
| } | |
| } | 
6. 使用CancellationToken进行取消操作
 
在多线程编程中,可能需要取消正在进行的长时间运行的操作。CancellationToken和CancellationTokenSource类提供了取消操作的功能。
csharp代码 
| using System; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| // 创建一个取消令牌源 | |
| CancellationTokenSource cts = new CancellationTokenSource(); | |
| // 启动一个可取消的任务 | |
| Task task = Task.Run(() => DoWork(cts.Token), cts.Token); | |
| // 在一段时间后取消任务 | |
| Thread.Sleep(2000); | |
| cts.Cancel(); | |
| } | |
| static void DoWork(CancellationToken token) | |
| { | |
| for (int i = 0; i < 10; i++) | |
| { | |
| token.ThrowIfCancellationRequested(); | |
| // 模拟工作 | |
| Thread.Sleep(500); | |
| Console.WriteLine($"Working... {i}"); | |
| } | |
| } | |
| } | 
在以上示例中,我们展示了如何在C#中使用不同的类和方法来创建和管理多线程应用程序。选择哪种方法取决于你的具体需求,例如任务的性质(I/O密集型还是CPU密集型