Thread 類用于創(chuàng)建和管理線程,表示托管線程,每個(gè)Thread對(duì)象都表示一個(gè)托管線程,每個(gè)托管線程都會(huì)對(duì)對(duì)應(yīng)一個(gè)函數(shù)。進(jìn)程:一個(gè)正在運(yùn)行的程序就是一個(gè)進(jìn)程。線程:操作系統(tǒng)為了提高效率將一個(gè)進(jìn)程分為多個(gè)線程。使用Thread開啟兩個(gè)線程同時(shí)運(yùn)行。按下執(zhí)行任務(wù)按鈕后,兩個(gè)任務(wù)同時(shí)執(zhí)行,互不影響。將屬性中的輸出類型改為:控制臺(tái)應(yīng)用程序。namespace _012_多線程Thread基礎(chǔ)
{
public partial class Thread基礎(chǔ) : Form
{
public Thread基礎(chǔ)()
{
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 編輯過