使用前須加入以下 NameSpace
using namespace System::Threading;
--------------------------------------
C++/CLR 主控台寫法+範例
void testa() { while(true) ::System::Console::WriteLine("aaaaa"); } void testb() { while(true) ::System::Console::WriteLine("bbbbb"); } void main() { Thread^ thr1 = gcnew ::Thread(gcnew::Threading::ThreadStart(testa)); Thread^ thr2 = gcnew ::Thread(gcnew::Threading::ThreadStart(testb)); thr1->Start(); thr2->Start(); }
---------說明--------
證明兩個無限迴圈的東西可以輪流同時執行
====================================================
C++/CLR From 表單寫法+範例(以 ListBox 作範例)
新增兩個測試用 Function
void testa() { for(int i=0; i<10; i++) this->listBox1->Items->Add("testa"); } void testb() { for(int i=0; i<10; i++) this->listBox1->Items->Add("testb"); } Form1_Load 事件內輸入 ::System::Threading::Thread^ thr1 = gcnew ::System::Threading::Thread(gcnew ::System::Threading::ThreadStart(this, &testth::Form1::testa)); ::System::Threading::Thread^ thr2 = gcnew ::System::Threading::Thread(gcnew ::System::Threading::ThreadStart(this, &testth::Form1::testb)); thr1->Start(); thr2->Start();
------說明------
會發現 testa 跟 testb 輪流印,而不是 testa 先印10次再換 testb,證明兩個執行緒是分別同時執行的
全站熱搜