Synchronize( )如何使用?

a456852 2003-10-13 10:07:20
线程中有一个自定函数AnsiString __fastcall Tthread::sonmfunction(int x,int y),它要调用MAIN线程中的函数,如何使用Synchronize( )让它同步执行?
...全文
367 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
我不懂电脑 2003-10-13
  • 打赏
  • 举报
回复
This example shows how to call a button’s click method in a thread-safe manner:

void __fastcall TMyThread::PushTheButton(void)

{
Button1->Click();
}

void __fastcall TMyThread::Execute()
{
...
Synchronize(PushTheButton);
...
}
jspxnet 2003-10-13
  • 打赏
  • 举报
回复
java 中表示 单线程执行.就是用一时间不会执行两个.
cb中应该也一样.
tigerhohoo(老虎不吃人) 的代码就是很好的例子
LanceX 2003-10-13
  • 打赏
  • 举报
回复
Synchronize()的参数就是一个函数指针,并且在BCB通常中调用方式是__fastcall。
如果有疑惑的话,直接看一下函数指针概念就行了。
不过从降低耦合度的角度来讲,我认为最好不要在线程中调用MAIN线程中的函数,你可以尝试将函数抽出到另外一个unit中。
另外,如果要实现线程和MAIN线程的交互,也可以用SendMessage发消息来做,个人观点认为要好一点。
tigerhohoo 2003-10-13
  • 打赏
  • 举报
回复
void __fastcall TScreenThread::Execute(void)
{
//Keep running until the thread is terminated
while (!Terminated)
{
//Drop into the context of the UI thread to write the message to the screen
Synchronize(UpdateScreen);
}
}
//----------------------------------------------------------------------------
void __fastcall TScreenThread::UpdateScreen(void)
{
//If there are too many messages of the screen, clear it
if (Form1->mMessages->Lines->Count > 200)
Form1->mMessages->Lines->Clear();
//Write the message to the screen
Form1->mMessages->Lines->Add(Message);
}
yhz 2003-10-13
  • 打赏
  • 举报
回复
首先,你的那个 sonmfunction 函数不能带有任何的参数,也不能有返回值。
如果你实在要参数或返回值的话,就用全局变量吧。
也就是说要定义成:void __fastcall Tthread::sonmfunction(void);

然后你就可以用 Synchronize(sonmfunction); 来调用它了。
GHOSTWY 2003-10-13
  • 打赏
  • 举报
回复

友情up
Wingsun 2003-10-13
  • 打赏
  • 举报
回复
你需要做一些特殊处理才能让synchronize可以调用你的函数。

13,825

社区成员

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

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