首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

线程还是有有关问题额

2013-01-04 
线程还是有问题额本帖最后由 maliang799 于 2011-09-07 14:07:34 编辑class TMyThread: public TThread{pr

线程还是有问题额
本帖最后由 maliang799 于 2011-09-07 14:07:34 编辑


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怎么做额,
[解决办法]
简单的事件的例子,消息的你先自己看看

#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

热点排行