2

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.

JP29
  • 633
  • 4
  • 13
  • 25
  • 1
    Is there something more specific that you're struggling with? If you just want an overview of the basics you can find the info on the net, e.g. http://www.temporaldoorway.com/programming/cbuilder/threads/basics.htm – Michael Jul 25 '12 at 15:34
  • 1
    Possible duplicate of [Threads in C++ builder](http://stackoverflow.com/questions/11639859/threads-in-c-builder). – Remy Lebeau Jul 26 '12 at 02:08
  • Please, provide some piece of code you are using, or be more specific on what do you want... – kokbira Sep 11 '12 at 19:37
  • Generally, you can launch some event or thread from your project.cpp, before Application->Run()...... http://stackoverflow.com/questions/3452089/how-do-i-set-my-mainform-to-be-hidden-when-my-program-starts – kokbira Sep 25 '12 at 14:08

1 Answers1

4

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!!";
}
Riwels
  • 786
  • 10
  • 21