⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample53.cs

📁 C#函数手册
💻 CS
字号:
namespace apiBook
{
	using System;
	using System.Threading;
	public class TestThreadClass 
	{
		public static void Main()
		{			
			bool flag=true;
			Thread testT1=new Thread(new ThreadStart(DoPrint));
			testT1.Start();
			while(flag)
			{
				if(testT1.IsAlive)
				{
					testT1.Suspend();
					//使用Suspend方法将线程挂起
					flag=false;
				}
			}
			Thread testT2=new Thread(new ThreadStart(DoRead));
			testT2.Start();
			//使用Start方法启动线程
			Console.WriteLine("读线程Abort");
			try
			{
				flag=true;
				while(flag)
				{
					if(testT2.IsAlive)
					{
						testT2.Abort();
						//使用Abort方法终止线程
						flag=false;
					}
				}			
			}
			catch(ThreadAbortException  e)
			{
				Console.WriteLine(e.ToString());
				Thread.ResetAbort();
				//使用ResetAbort方法

			}
		
			Console.WriteLine("取消当前Abort请求。");
			//Thread.ResetAbort();
			//Console.WriteLine("中断读线程。");
			testT2.Interrupt();
			//使用Interrupt方法中断线程
		
			Console.WriteLine("让打印线程继续运行。");
			testT1.Resume();
			//使用Resume方法继续挂起的线程
			Console.WriteLine("重新开始中断的读线程。");
			testT1.Join();
			//使用Join方法
			testT2.Join();
			Console.ReadLine();
		}	
		public static void DoRead()
		{
			Console.WriteLine("开始读线程。");
			Console.WriteLine("让线程等待200:");
			Thread.SpinWait(200);
		}
		public static void DoPrint()
		{
			Console.WriteLine("开始打印线程内容:");
			Console.WriteLine("打印线程Sleep300毫秒");
		
			Thread.Sleep(300);
			Console.WriteLine("继续打印线程GetDomain="+Thread.GetDomain().ToString());
			Console.WriteLine("继续打印线程GetDomainID="+Thread.GetDomainID().ToString());		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -