重新开帖,高分求救,线程同步WaitFor问题,,急!!
下面这段代码BUTTON单击后为什么会报错,说句柄无效
***************unit1.h-------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
--------------------------
************unit1.cpp------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyThread *thread;
thread=new MyThread(false);
if(thread->WaitFor()==0)
ShowMessage("return");
}
***************unit2.h-----------------
class MyThread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall MyThread(bool CreateSuspended);
};
***********unit2.cpp----------------
__fastcall MyThread::MyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall MyThread::Execute()
{
//---- Place thread code here ----
FreeOnTerminate=true;
ReturnValue=255;
int sum;
for(int i=0;i<200000;i++)
sum+=i;
ReturnValue=0;
}