• 大小: 22KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-24
  • 语言: C#
  • 标签: 动态创建  需要关闭  

资源简介

#>【C#多线程】 动态创建多个线程后 需要关闭其中某个线程的解决实例。 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("创建 Thread" + i); listThread.Add(thread); thread.Start(i); Thread.Sleep(i * 100); } //关闭指定线程 foreach (Thread tempThread in listThread) { if (tempThread.Name == "Thread3") { Console.WriteLine(tempThread.Name + " 线程已关闭"); 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(); } 如何关闭其中的一个线程呢? 请下载解决方案。 呵呵,这个实例10分值的买, 觉得好的话就评个分吧,评分后会返回你11分 (一定要评分,光评论是不返分的)

资源截图

代码片段和文件信息

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(“创建 Thread“ + i);
                listThread.Add(thread);
                thread.Start(i);
                Thread.Sleep(i * 100);
            }

            //关闭指定线程
            foreach (Thread tempThread in listThread)
            {
                if (tempThread.Name == “Thread3“)
                {
                    Console.WriteLine(tempThread.Name + “ 线程已关闭“);
                    tempThread.Abort();
                }
            }

            Console.ReadLine();
        }

        private static void ThreadMethod(object t)
        {
            Thread.Sleep(1500);
            Console.WriteLine(String.Format(“我是 Thread{0}“ t));
        }
    }


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       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


评论

共有 条评论