-
大小: 22KB文件類型: .rar金幣: 2下載: 1 次發(fā)布日期: 2021-06-24
- 語(yǔ)言: C#
- 標(biāo)簽: 動(dòng)態(tài)創(chuàng)建??需要關(guān)閉??
資源簡(jiǎn)介
#>【C#多線程】 動(dòng)態(tài)創(chuàng)建多個(gè)線程后 需要關(guān)閉其中某個(gè)線程的解決實(shí)例。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List listThread = new List(5);
Thread thread = null;
for (int i = 0; i < 5; i++)
{
thread = new Thread(ThreadMethod);
thread.Name = "Thread" + i;
Console.WriteLine("創(chuàng)建 Thread" + i);
listThread.Add(thread);
thread.Start(i);
Thread.Sleep(i * 100);
}
//關(guān)閉指定線程
foreach (Thread tempThread in listThread)
{
if (tempThread.Name == "Thread3")
{
Console.WriteLine(tempThread.Name + " 線程已關(guān)閉");
tempThread.Abort();
}
}
Console.ReadLine();
}
private static void ThreadMethod(object t)
{
Thread.Sleep(1500);
Console.WriteLine(String.Format("我是 Thread{0}", t));
}
}
}
for (int i = 0; i < 5; i++)
{
Thread thread = new Thread(test) ;
thread.Start();
}
如何關(guān)閉其中的一個(gè)線程呢? 請(qǐng)下載解決方案。
呵呵,這個(gè)實(shí)例10分值的買,
覺得好的話就評(píng)個(gè)分吧,評(píng)分后會(huì)返回你11分 (一定要評(píng)分,光評(píng)論是不返分的)

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Threading;
namespace?ConsoleApplication2
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????List?listThread?=?new?List(5);
????????????Thread?thread?=?null;
????????????for?(int?i?=?0;?i?5;?i++)
????????????{
????????????????thread?=?new?Thread(ThreadMethod);
????????????????thread.Name?=?“Thread“?+?i;
????????????????Console.WriteLine(“創(chuàng)建?Thread“?+?i);
????????????????listThread.Add(thread);
????????????????thread.Start(i);
????????????????Thread.Sleep(i?*?100);
????????????}
????????????//關(guān)閉指定線程
????????????foreach?(Thread?tempThread?in?listThread)
????????????{
????????????????if?(tempThread.Name?==?“Thread3“)
????????????????{
????????????????????Console.WriteLine(tempThread.Name?+?“?線程已關(guān)閉“);
????????????????????tempThread.Abort();
????????????????}
????????????}
????????????Console.ReadLine();
????????}
????????private?static?void?ThreadMethod(object?t)
????????{
????????????Thread.Sleep(1500);
????????????Console.WriteLine(String.Format(“我是?Thread{0}“?t));
????????}
????}
}?
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????5632??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
?????文件??????13824??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb
?????文件??????11608??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe
?????文件????????490??2010-03-17?22:39??ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.manifest
?????文件???????2207??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.csproj
?????文件????????594??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.csproj.FileListAbsolute.txt
?????文件???????5632??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.exe
?????文件??????13824??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.pdb
?????文件???????5095??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????1214??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\Program.cs
?????文件???????1388??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\Properties\AssemblyInfo.cs
?????文件????????899??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1.sln
????..A..H.?????13824??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1.suo
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\obj\x86\Debug\TempPE
?????目錄??????????0??2010-09-09?08:34??ConsoleApplication1\ConsoleApplication1\obj\x86\Debug
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\bin\Debug
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\obj\x86
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\bin
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\obj
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1\Properties
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1\ConsoleApplication1
?????目錄??????????0??2010-09-09?08:20??ConsoleApplication1
-----------?---------??----------?-----??----
????????????????76231????????????????????22
評(píng)論
共有 條評(píng)論