C#中关于多个线程的启动与停止
for(inti=0;i<5;i++){ThreadThrRun=newThread(StartRun);ThrRun.Start();}如上代码,我启动了5个线程,在程...
for (int i = 0; i < 5; i++)
{
Thread ThrRun = new Thread(StartRun) ;
ThrRun.Start();
}
如上代码,我启动了5个线程,在程序运行的过程中 如何关闭其中的某一个线程呢? 展开
{
Thread ThrRun = new Thread(StartRun) ;
ThrRun.Start();
}
如上代码,我启动了5个线程,在程序运行的过程中 如何关闭其中的某一个线程呢? 展开
2个回答
展开全部
希望代码对你陵携有帮助
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
List<Thread> listThread = new List<Thread>(5);
Thread thread = null;
for (int i = 0; i < 5; i++)
{
thread = new Thread(new ThreadStart(p.ThreadMethod));
thread.Name = "Thread" + (i + 1);
Console.WriteLine("创建 Thread" + (i + 1));
listThread.Add(thread);
}
//关棚汪拍闭指定线程
foreach (Thread tempThread in listThread)
{
if (tempThread.Name == "Thread3")
{
Console.WriteLine(tempThread.Name + " 线程已关链羡闭");
tempThread.Abort();
}
}
Console.ReadLine();
}
private void ThreadMethod()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
List<Thread> listThread = new List<Thread>(5);
Thread thread = null;
for (int i = 0; i < 5; i++)
{
thread = new Thread(new ThreadStart(p.ThreadMethod));
thread.Name = "Thread" + (i + 1);
Console.WriteLine("创建 Thread" + (i + 1));
listThread.Add(thread);
}
//关棚汪拍闭指定线程
foreach (Thread tempThread in listThread)
{
if (tempThread.Name == "Thread3")
{
Console.WriteLine(tempThread.Name + " 线程已关链羡闭");
tempThread.Abort();
}
}
Console.ReadLine();
}
private void ThreadMethod()
{
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询