如果有人能帮助我,我会很高兴。我自己是C ++ Builder的新手,从未在C ++中使用过线程。
我在c ++ builder中有一个表单我想要线程化,所以它不会崩溃。目前,在完成应用程序的后台进程之前,表单不会加载。
答案 0 :(得分:4)
在C ++ Builder中,您应该添加一个Thread对象(右键单击“project.exe”,添加new,other。它位于C ++ Builder文件夹中)。 然后,您需要添加标头包含并实例化对象。
如果你太忙于处理对象,你可以简单地将CreateThread函数与函数一起使用。也许这不是最好的,但如果你没有经验就很容易。
TForm1 *Form1;
unsigned long __stdcall my_thread_func(void *args);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner){
CreateThread(NULL,0,&my_thread_func,NULL,0,NULL); //create thread in form constructor
}
//---------------------------------------------------------------------------
// Write a function like this
unsigned long __stdcall my_thread_func(void *args){
Sleep(5000);
Form1->Caption = L"Done!!";
}