在本文中,我将解释如何通过在.NET Framework中使用Thread.Abort方法中止线程。
在C#中中止线程非常简单。
当我们希望中止程序中的线程时使用它。
调用Thread类的中止方法以中止线程。
确保在中止之前检查Isalive属性。
if (Thread.IsAlive)
{
Thread.Abort();
}
在C#中终止线程示例
如何终止C#中的线程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Threadabort
{
class Program
{
static void Main(string[] args)
{
Thread th = new Thread(WriteY);
if (th.IsAlive)
{
th.Start();
th.Abort();
}
for (int i = 0; i <= 10; i++)
{
Console.WriteLine("Hello");
}
Console.ReadLine();
}
static void WriteY()
{
for (int i = 0; i <= 9; i++)
{
Console.WriteLine("Hi");
}
}
}
}
日期:2020-04-11 22:50:13 来源:oir作者:oir
