如何在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
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短