如何在 C# 中创建线程?
线程是轻量级的进程。线程被定义为程序的执行路径。通过扩展 Thread 类创建线程。然后,扩展的 Thread 类调用 Start() 方法来开始子线程执行。
线程示例:一个常见的线程使用示例是指由现代操作系统实施的并发编程。使用线程可以节省 CPU 周期浪费,并提高应用程序的效率。
以下是示范如何创建线程的一个示例。
示例
using System;
using System.Threading;
namespace Demo {
class Program {
public static void ThreadFunc() {
Console.WriteLine("Child thread starts");
}
static void Main(string[] args) {
ThreadStart childref = new ThreadStart(ThreadFunc);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadKey();
}
}
}输出
In Main: Creating the Child thread Child thread starts
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP