เข้าร่วม
บล็อกเธรดที่เรียกจนกว่าเธรดจะสิ้นสุดลง ในขณะที่ดำเนินการสูบ COM มาตรฐานและ SendMessage แบบมาตรฐานต่อไป วิธีนี้มีรูปแบบโอเวอร์โหลดที่แตกต่างกัน
นอนหลับ
ทำให้เธรดหยุดชั่วคราวเป็นระยะเวลาหนึ่ง
ยกเลิก
วิธีการ Abort ใช้เพื่อทำลายเธรด
ให้เราดูตัวอย่างของ Join() ใน threading -
ตัวอย่าง
using System;
using System.Diagnostics;
using System.Threading;
namespace Sample {
class Demo {
static void Run() {
for (int i = 0; i < 2; i++)
Console.Write("Sample text!");
}
static void Main(string[] args) {
Thread t = new Thread(Run);
t.Start();
t.Join();
Console.WriteLine("Thread terminated!");
Console.Read();
}
}
} ให้เรามาดูตัวอย่าง abort() และ sleep() ใน Threading
ตัวอย่าง
using System;
using System.Threading;
namespace Demo {
class ThreadCreationProgram {
public static void CallToChildThread() {
try {
Console.WriteLine("Child thread starts");
// do some work, like counting to 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();
//stop the main thread for some time
Thread.Sleep(2000);
//now abort the child
Console.WriteLine("In Main: Aborting the Child thread");
childThread.Abort();
Console.ReadKey();
}
}
}