线程还是有问题额

茹果伱在 2011-09-07 02:04:36

class TMyThread: public TThread
{
private:
int *a;
void function();
protected:
void __fastcall Execute();
public:
__fastcall TMyThread(bool CreateSuspended, int *a1);
};

__fastcall TMyThread::TMyThread(bool CreateSuspended, int *a1)
:TThread(CreateSuspended)
{
a = a1;
}
void __fastcall TMyThread::Execute()
{
while(!Terminated)
function();
}

void TMyThread::function()
{
a[0] = 10;
Terminate();
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{
int a[20];
memset(a, 0, sizeof(a));
TMyThread *thread = new TMyThread(false, a);
//Sleep(10);
ShowMessage(a[0]);
}

得到的是0。。我想要及时得到10怎么做额,
...全文
79 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Bosman 2011-09-13
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int a[20];
memset(a, 0, sizeof(a));
TMyThread *thread = new TMyThread(false, a);
thread->WaitFor();//线程必须不耗时,不然主界面会卡
ShowMessage(a[0]);//这个函数这样用的??

}
回复
茹果伱在 2011-09-08
学习学习先。。
回复
tianlu123 2011-09-07
赞一下我们的波波!!!
回复
zzbinfo 2011-09-07
简单的事件的例子,消息的你先自己看看

#include <vcl.h>
#pragma hdrstop

#include "Unit2.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,int *a1,TEvent *Complete)
: TThread(CreateSuspended)
{
a = a1;
m_Complete = Complete;
FreeOnTerminate = true;
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
//---- Place thread code here ----
while(!Terminated)
function();

}
//---------------------------------------------------------------------------
void TMyThread::function()
{
a[0] = 10;
m_Complete->SetEvent();
Terminate();
}

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TMyThread : public TThread
{
private:
int *a;
void function();
TEvent *m_Complete;
protected:
void __fastcall Execute();
public:
__fastcall TMyThread(bool CreateSuspended,int *a1,TEvent *Complete);
};
//---------------------------------------------------------------------------
#endif

#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)
{
int a[20];
memset(a, 0, sizeof(a));
m_Complete = new TEvent(NULL,true,true,"ThreadEvent",true);
m_Complete->ResetEvent();
TMyThread *thread = new TMyThread(false, a,m_Complete);
m_Complete->WaitFor(INFINITE);
//Sleep(10);
ShowMessage(a[0]);
delete m_Complete;

}
//---------------------------------------------------------------------------


#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
TEvent *m_Complete;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

回复
茹果伱在 2011-09-07
具体怎么做额 不太懂
回复
zzbinfo 2011-09-07
你这个要弄个事件在线程中对数据处理完了后,通知主线程数据准备好,或者用消息通知也可以
回复
bigfog 2011-09-07
通过 发消息
回复
相关推荐
发帖
网络及通讯开发
创建于2007-08-02

1299

社区成员

C++ Builder 网络及通讯开发
申请成为版主
帖子事件
创建了帖子
2011-09-07 02:04
社区公告
暂无公告