C++构建器中的线程

Amy*_*y L 6 c++ c++builder

我是c ++ builder的新手,不熟悉线程,我希望有人可以发布一个例子或指出我正确的方向.

我有一个formShow()在c ++ builder中加载函数的表单.它做我想要我的程序做的事情,但只有在那之后它才会显示实际的表格.

为此我需要线程化程序的表单和后台运行.任何人都可以帮助我吗?

Rem*_*eau 8

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);
}
Run Code Online (Sandbox Code Playgroud)

如果你真的想要编写代码,你可以这样做:

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 ...
}
Run Code Online (Sandbox Code Playgroud)