简体   繁体   中英

T Thread in C++ Builder

I would be happy if anyone could help me. I am new to C++ Builder myself and never used threading in C++.

I have a form within c++ builder I want to thread so it does not crash. At the moment the form does not load until it has completed the background processes of the application.

In C++ Builder, you should add a Thread Object (Right click on "project.exe", add new, other. It's on C++ Builder files folder). Then you need to add the header include and instantiate the object.

If you are too noob to deal with the object, you can simply use the CreateThread function with a function. Maybe it's not the best, but it's very easy if you are not experienced.

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!!";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM