C++编译器中的线程
Threads in C++ builder
我是c++构建器的新手,不熟悉线程,我希望有人能发布一个例子或为我指明正确的方向。
我有一个在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 ...
}
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 我怎么知道C++编译器是否制作线程安全的静态对象代码
- 在Windows 10上使用C 中的线程(使用G 作为编译器)的问题
- 尝试使用std ::线程时编译器错误
- 编译器优化是否解决了线程安全问题
- 可以(通过编译器)使用多少个线程来初始化全局对象(函数main之前)
- STD线程呼叫模板模板类的成员功能:编译器错误
- C++编译器中的线程
- 对于 c++11 之前的编译器,返回本地静态对象线程是否安全
- 计时器、线程和编译器不当行为
- 线程池的 lambda 函数内的编译器错误变量"未捕获"
- C++编译器:为不同线程整理的接口
- 多线程可能会抑制编译器优化
- GCC-Visual Studio std::线程编译器的差异
- 现在的C和C++编译器的线程保证是什么
- C++ 编译器如何保证常量成员变量的线程安全
- visual studio 2010 -是否有办法在旧的C/ c++编译器中使用c++ 11线程对象?