Thread 類用于創建和管理線程,表示托管線程,每個Thread對象都表示一個托管線程,每個托管線程都會對對應一個函數。線程:操作系統為了提高效率將一個進程分為多個線程。使用Thread開啟兩個線程同時運行。按下執行任務按鈕后,兩個任務同時執行,互不影響。namespace _012_多線程Thread基礎
{
public partial class Thread基礎 : Form
{
public Thread基礎()
{
InitializeComponent();
}
private void btTask1_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(()=>
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine((10 + i) + " ");
Thread.Sleep(1000);
}
});
thread1.IsBackground = true;
thread1.Start();
}
private void btTask2_Click(object sender, EventArgs e)
{
Thread thread2 = new Thread(() =>
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine((100 + i) + " ");
Thread.Sleep(1000);
}
});
thread2.IsBackground = true;
thread2.Start();
}
}
}
閱讀原文:原文鏈接
該文章在 2025/3/31 11:26:12 編輯過