为了创建线程,我创建了一个函数 -
public void myThread() { for (int i = 0; i < 3; i++) { Console.WriteLine("My Thread"); } }
调用上面的函数来创建一个线程,并创建一个新的 ThreadStart 委托 -
Demo d = new Demo(); Thread thread = new Thread(new ThreadStart(d.myThread));
使用以下代码创建一个简单的线程。
实时演示
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; class Demo { public void myThread() { for (int i = 0; i < 3; i++) { Console.WriteLine("My Thread"); } } } class NewThread { public static void Main() { Demo d = new Demo(); Thread thread = new Thread(new ThreadStart(d.myThread)); thread.Start(); Console.Read(); } }
My Thread My Thread My Thread
以上就是创建简单线程的 C# 程序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号