C# 多线程 System.Threading.Thread

主线程

在 C# 中,System.Threading.Thread 类用于线程的工作。它允许创建并访问多线程应用程序中的单个线程。进程中第一个被执行的线程称为主线程。

当 C# 程序开始执行时,主线程自动创建。使用 Thread 类创建的线程被主线程的子线程调用。您可以使用 Thread 类的 CurrentThread 属性访问线程。

下面的程序演示了主线程的执行:

using System;
using System.Threading;

namespace MultithreadingApplication
{
    class MainThreadProgram
    {
        static void Main(string[] args)
        {
            Thread th = Thread.CurrentThread;
            th.Name = "MainThread";
            Console.WriteLine("This is {0}", th.Name);
            Console.ReadKey();
        }
    }
}

当上面的代码被编译和执行时,它会产生下列结果:

This is MainThread

Thread 类常用的属性和方法

下表列出了 Thread 类的一些常用的 属性:

属性	                描述
CurrentContext	     获取线程正在其中执行的当前上下文。
CurrentCulture	     获取或设置当前线程的区域性。
CurrentPrinciple	 获取或设置线程的当前负责人(对基于角色的安全性而言)。
CurrentThread	     获取当前正在运行的线程。
CurrentUICulture	 获取或设置资源管理器使用的当前区域性以便在运行时查找区域性特定的资源。
ExecutionContext	 获取一个 ExecutionCon text 对象,该对象包含有关当前线程的各种上下文的信息。
IsAlive	             获取一个值,该值指示当前线程的执行状态。
IsBackground	     获取或设置一个值,该值指示某个线程是否为后台线程。
IsThreadPoolThread   获取一个值,该值指示线程是否属于托管线程池。
ManagedThreadId	     获取当前托管线程的唯一标识符。
Name	             获取或设置线程的名称。
Priority	         获取或设置一个值,该值指示线程的调度优先级。
ThreadState	         获取一个值,该值包含当前线程的状态。


 Task.Run(() =>
            {
                Task.WaitAll(taskGetMat);
                //计算需求数量
                var lstMaterialStocks = JsonData<List<Material_Stock>>(taskGetMat.Result);
                //lstMaterialStocks.ForEach(p => p.RequireQty = p.MachineCount * p.UseCount - p.InventoryQty);
                if (lstMaterialStocks != null)
                {
                    lstMaterialStocks.ForEach(p => p.RequireQty = ((p.MachineCount * p.UseCount - p.InventoryQty) > 0 ? (p.MachineCount * p.UseCount - p.InventoryQty) : 0));
                }
                if (this == null || this.IsDisposed)
                {
                    CloseWaitingDialog();
                    return;
                }
                this.Invoke(new Action(() =>
                {
                    CloseWaitingDialog();

                    this.treeList1.DataSource = lstMaterialStocks;
                    this.treeList1.ParentFieldName = "parentID";
                    this.treeList1.KeyFieldName = "keyID";
                    MarkSelectedMaterial(this.treeList1.Nodes);
                }));
            });

创建线程

线程是通过扩展 Thread 类创建的。扩展的 Thread 类调用 Start() 方法来开始子线程的执行。

下面的程序演示了这个概念:

using System;
using System.Threading;

namespace MultithreadingApplication
{
    class ThreadCreationProgram
    {
        public static void CallToChildThread()
        {
            Console.WriteLine("Child thread starts");
        }
        
        static void Main(string[] args)
        {
            ThreadStart childref = new ThreadStart(CallToChildThread);
            Console.WriteLine("In Main: Creating the Child thread");
            Thread childThread = new Thread(childref);
            childThread.Start();
            Console.ReadKey();
        }
    }
}

当上面的代码被编译和执行时,它会产生下列结果:

In Main: Creating the Child thread
Child thread starts

管理线程

Thread 类提供了各种管理线程的方法。

下面的实例演示了 sleep() 方法的使用,用于在一个特定的时间暂停线程。

using System;
using System.Threading;

namespace MultithreadingApplication
{
    class ThreadCreationProgram
    {
        public static void CallToChildThread()
        {
            Console.WriteLine("Child thread starts");
            // 线程暂停 5000 毫秒
            int sleepfor = 5000; 
            Console.WriteLine("Child Thread Paused for {0} seconds", 
                              sleepfor / 1000);
            Thread.Sleep(sleepfor);
            Console.WriteLine("Child thread resumes");
        }
        
        static void Main(string[] args)
        {
            ThreadStart childref = new ThreadStart(CallToChildThread);
            Console.WriteLine("In Main: Creating the Child thread");
            Thread childThread = new Thread(childref);
            childThread.Start();
            Console.ReadKey();
        }
    }
}

当上面的代码被编译和执行时,它会产生下列结果:

In Main: Creating the Child thread
Child thread starts
Child Thread Paused for 5 seconds
Child thread resumes

销毁线程

Abort() 方法用于销毁线程。

通过抛出 threadabortexception 在运行时中止线程。这个异常不能被捕获,如果有 finally 块,控制会被送至 finally 块。

下面的程序说明了这点:

using System;
using System.Threading;

namespace MultithreadingApplication
{
    class ThreadCreationProgram
    {
        public static void CallToChildThread()
        {
            try
            {

                Console.WriteLine("Child thread starts");
                // 计数到 10
                for (int counter = 0; counter <= 10; counter++)
                {
                    Thread.Sleep(500);
                    Console.WriteLine(counter);
                }
                Console.WriteLine("Child Thread Completed");

            }
            catch (ThreadAbortException e)
            {
                Console.WriteLine("Thread Abort Exception");
            }
            finally
            {
                Console.WriteLine("Couldn't catch the Thread Exception");
            }

        }
        
        static void Main(string[] args)
        {
            ThreadStart childref = new ThreadStart(CallToChildThread);
            Console.WriteLine("In Main: Creating the Child thread");
            Thread childThread = new Thread(childref);
            childThread.Start();
            // 停止主线程一段时间
            Thread.Sleep(2000);
            // 现在中止子线程
            Console.WriteLine("In Main: Aborting the Child thread");
            childThread.Abort();
            Console.ReadKey();
        }
    }
}

当上面的代码被编译和执行时,它会产生下列结果:

In Main: Creating the Child thread
Child thread starts
0
1
2
In Main: Aborting the Child thread
Thread Abort Exception
Couldn't catch the Thread Exception 

猜你喜欢

转载自blog.csdn.net/lemonemmm/article/details/82252063
今日推荐