C# 如何解决程序卡顿的问题(多线程)
在编写程序的时候,有时候难免会出现后台运行时间过长的问题,这个时候就要考虑多线程的操作了。
正文
不带参数的多线程实现
第一步 建立控制台应用
第二步 引用System.Threading.Thread
using System.Threading;
在 C# 中,System.Threading.Thread 类用于线程的工作。它允许创建并访问多线程应用程序中的单个线程。进程中第一个被执行的线程称为主线程。
第三步:完成代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace 多线程Test
{
class Program
{
static void Main(string[] args)
{
int num = 100;
for (int i = 0; i < num; i++)
{
//无参的多线程
noParmaThread();
}
}
private static void StartThread()
{
Console.WriteLine("------开始了新线程------");
Thread.Sleep(2000);//wait
Console.WriteLine("------线程结束------");
}
/// <summary>
///不需要传递参数
/// </summary>
private static void noParmaThread()
{
ThreadStart threadStart = new ThreadStart(StartThread);
var thread = new Thread(threadStart);
thread.Start();//开始线程
}
}
}