6
我是C++ builder的新手,對線程不熟悉我希望有人可以發表一個示例或指向正確的方向。C++ builder中的線程
我有一個窗體,在C++ builder中加載formShow()
函數。它做我希望我的程序要做的事情,但只有在它之後纔會顯示實際的表單。
爲此我需要線程的形式和程序的後臺運行。任何人都可以幫助我嗎?
我是C++ builder的新手,對線程不熟悉我希望有人可以發表一個示例或指向正確的方向。C++ builder中的線程
我有一個窗體,在C++ builder中加載formShow()
函數。它做我希望我的程序要做的事情,但只有在它之後纔會顯示實際的表單。
爲此我需要線程的形式和程序的後臺運行。任何人都可以幫助我嗎?
在OnShow
事件退出之前,可能只是簡單地延遲邏輯,而不使用線程。例如:
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);
}
如果你真的想線程的代碼,你可以做這樣的:
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 ...
}