2012-07-24 15 views
6

Sono nuovo al builder C++ e non conosco il threading Speravo che qualcuno potesse pubblicare un esempio o indicarmi la giusta direzione.Thread in C++ builder

Ho un modulo che carica la funzione formShow() in C++ builder. Fa ciò che voglio che il mio programma faccia, ma solo dopo mostrerà la forma attuale.

Per questo ho bisogno di passare il modulo e lo sfondo in esecuzione del programma. Qualcuno può aiutarmi atall?

risposta

8

Potrebbe essere più semplice ritardare la logica fino a quando non è terminato l'evento OnShow, senza utilizzare un thread. Per esempio:

const UINT WM_DO_WORK = WM_USER + 1; 

void __fastcall TForm1::FormShow(TObject *Sender) 
{ 
    PostMessage(Handle, WM_DO_WORK, 0, 0); 
} 

void __fastcall TForm1::WndProc(TMessage &Message) 
{ 
    if (Message.Msg == WM_DO_WORK) 
    { 
     // do work here ... 
    } 
    else 
     TForm::WndProc(Message); 
} 

Se davvero si vuole infilare il codice, è possibile farlo in questo modo:

class TMyThread : public TThread 
{ 
protected: 
    virtual void __fastcall Execute(); 
public: 
    __fastcall TMyThread(); 
}; 

__fastcall TMyThread::TMyThread() 
    : TThread(true) 
{ 
    FreeOnTerminate = true; 
    // setup other thread parameters as needed... 
} 

void __fastcall TMyThread::Execute() 
{ 
    // do work here ... 
    // if you need to access the UI controls, 
    // use the TThread::Synchornize() method for that 
} 

void __fastcall TForm1::FormShow(TObject *Sender) 
{ 
    TMyThread *thrd = new TMyThread(); 
    thrd->OnTerminate = &ThreadTerminated; 
    thrd->Resume(); 
} 

void __fastcall TForm1::ThreadTerminated(TObject *Sender) 
{ 
    // thread is finished with its work ... 
}