using System;
using System.Threading;
using System.Security;
namespace C17_5
{
public class MyThread
{
public void TestThread()
{
for (int i = 0; i < 3; i++)
{
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name+"="+i);
try
{
Thread.Sleep(1);
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e.Message);
}
catch (SecurityException e)
{
Console.WriteLine(e.Message);
}
}
}
}
public class Test
{
public static void Main()
{
Console.WriteLine("開始啟動執行緒...");
MyThread myThread = new MyThread();
Thread t1 = new Thread(new ThreadStart(myThread.TestThread));
Thread t2 = new Thread(new ThreadStart(myThread.TestThread));
t1.Name ="執行緒A";
t2.Name ="執行緒B";
Console.WriteLine("執行緒A啟動");
t1.Start();
Console.WriteLine("執行緒A終止");
t1.Abort();
Console.WriteLine("執行緒B啟動");
t2.Start();
Console.WriteLine("執行緒B終止");
t2.Abort();
}
}
}
沒有留言:
張貼留言