系统学习BCB之多线程篇 可以不在主线程里执行VCL组件吗

天下云飞 2003-07-23 11:38:34
看了一下
好像是说要使用VCL组件必须用Synchronize(doIt)同步函数
而同步函数实际上是在主线程中执行

不会吧
如果想使用数据库控件更新数据这样长时操作
主线程界面就死掉的话?
这还有什么搞头
线程里面只能做些数值计算???
这也太逊了吧
...全文
64 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2004-01-31
  • 打赏
  • 举报
回复
mark
yesry 2003-07-25
  • 打赏
  • 举报
回复
我觉得任何类的操作都不是线程安全的,因为都有读后写等问题。例如微软的CPen->CreatePen()就不能线程安全。

但是,如果各个线程都是用不同的内存,那就没有问题了。
例如:



//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TLabel *Label2;
TButton *Button1;
TButton *Button2;
TLabel *Label3;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
class update : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall update(bool CreateSuspended,TLabel *l);

TLabel *label;
};
//---------------------------------------------------------------------------
#endif




//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
HANDLE hMutex=NULL;
__fastcall update::update(bool CreateSuspended,TLabel *l)
: TThread(CreateSuspended)
{
label=l;
if(hMutex==NULL)hMutex=CreateMutex(NULL,false,NULL);
}
//---------------------------------------------------------------------------
void __fastcall update::Execute()
{
int i=0;
randomize();
int t=rand()%1000;
while(!this->Terminated)
{
Sleep(t);
label->Caption=i++;//这是不用同步的。因为使用了不同的VCL
if(WaitForSingleObject(hMutex,2000)==WAIT_OBJECT_0)//同步一下
{
Form1->Label1->Caption=i++;
ReleaseMutex(hMutex);
}
}
if(hMutex==NULL){CloseHandle(hMutex);hMutex=NULL;}
delete this;
}
//---------------------------------------------------------------------------
update *u1,*u2;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
u1=new update(false,Label2);
Sleep(2000);
u2=new update(false,Label3);
}
//---------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender)

void __fastcall TForm1::Button2Click(TObject *Sender)
{
u1->Terminate();
u2->Terminate();
}
//---------------------------------------------------------------------------
天下云飞 2003-07-25
  • 打赏
  • 举报
回复
up
asimpleman 2003-07-24
  • 打赏
  • 举报
回复
gz
snla 2003-07-24
  • 打赏
  • 举报
回复
不是星星,洗耳恭听!
天下云飞 2003-07-24
  • 打赏
  • 举报
回复
刚才试了一下
一不小心往数据库里插了1000条数据
不过感觉挺好,哈哈

Ado,DbExpress是线程安全的就好
别的我都很少用了
我不懂电脑 2003-07-24
  • 打赏
  • 举报
回复
数据集控件是线程安全的。
天下云飞 2003-07-24
  • 打赏
  • 举报
回复
matq2008(叶子.net)
刚才看看你不是已经是一颗星了么
恭喜恭喜啊
天下云飞 2003-07-24
  • 打赏
  • 举报
回复
在线程内new TForm1
总是报错canvas does not allow drawing
这是怎么一回事
在线程内可以创建窗体吗
需要做些什么事
叶子哟 2003-07-23
  • 打赏
  • 举报
回复
安全不安全一试就知呀! :)
天下云飞 2003-07-23
  • 打赏
  • 举报
回复
是吗,只是VCL的UI组件需要同步
仔细实验实验
ThinkX 2003-07-23
  • 打赏
  • 举报
回复
VCL的UI组件一般不是线程安全的,所以需要同步一下。
Synchronize其实是象主线程发送一个消息,利用消息队列来同步。
jintaocom 2003-07-23
  • 打赏
  • 举报
回复
呵呵。agree xu_xixnyu!
xu_xinyu 2003-07-23
  • 打赏
  • 举报
回复
有时候听一听大侠们的讲解会清楚一些。
TR@SOE 2003-07-23
  • 打赏
  • 举报
回复
贴主没有仔细看帮助。

如果你看了帮助,你应该知道大部分数据库控件还是线程安全的。
猎人66 2003-07-23
  • 打赏
  • 举报
回复
非也,数据库操作也可以在线程里边的啊
主要是VCL界面组件的更新应该放到Synchronize(doIt)里边

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧