线程中使用Synchronize函数后编译报错
我在Execute()中使用Synchronize()函数
编译时提示[C++ Error] Unit2.cpp(36): E2285 Could not find a match for 'TThread::Synchronize(void)'
请问是什么原因。代码如下
void __fastcall TMyThread::Execute()
{
for (int i = 0; i <= 200; i++)
{
strResult=IntToStr(i);
Synchronize(Result);
//edResult->Text = strResult ;
Sleep(100);
}
}
void __fastcall TMyThread::Result(void)
{
edResult->Text = strResult ;
}
[解决办法]
void __fastcall TMyThread::Result(void)
{
edResult->Text = strResult ;
}
放到Excute的前面。
在头文件里定义这个Result
[解决办法]
BCB2006?
Synchronize(&Result);
[解决办法]
lz 看这些代码是没有问题了 你得检查一下
我按你的意思做了个例子给你
//---------------------------------------//单元#include "Unit1.h"#include <vcl.h>#pragma hdrstop#include "Unit1.h"#include "Unit2.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ TMyThread *MyThread =new TMyThread(false); }//---------------------------------------//单元#include "Unit2.h"//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit2.h"#include "Unit1.h"#pragma package(smart_init)//---------------------------------------// Important: Methods and properties of objects in VCL can only be// used in a method called using Synchronize, for example://// Synchronize(UpdateCaption);//// where UpdateCaption could look like://// void __fastcall TMyThread::UpdateCaption()// {// Form1->Caption = "Updated in a thread";// }//---------------------------------------__fastcall TMyThread::TMyThread(bool CreateSuspended) : TThread(CreateSuspended){}//---------------------------------------void __fastcall TMyThread::Execute(){ //---- Place thread code here ---- for (int i = 0; i <= 200; i++) { strResult=IntToStr(i); Synchronize(Result); }}//---------------------------------------void __fastcall TMyThread::Result(void){ Form1->Edit1->Text = strResult ;}//.hpublic: __fastcall TMyThread(bool CreateSuspended); void __fastcall TMyThread::Result(void); //添加 AnsiString strResult ; //添加