我使用Multithread方式監看目錄,但會執行(run)時出問題,但在Debug模式又不會出問題,說實在這問題卡很久了,各位高手可以幫幫我嗎?因為我不知道錯在哪
程式大致上就是利用thread監看一個子目錄,一但此目錄下有檔案,則把他叫起來讀,但為何我用run就會出錯誤訊息,但是使用debug模式卻又一切正常??這是爲什麼??
先列出主程式之後再列Thread程式
//==================================================================
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE inputhandle;
AnsiString CurrentDir;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Memo1->Text="";
CurrentDir=GetCurrentDir();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
AnsiString Inputpath=CurrentDir "\\input";
inputhandle=FindFirstChangeNotification(Inputpath.c_str(),false,FILE_NOTIFY_CHANGE_FILE_NAME); //Thread啟動
TThreadInput *ThreadInput=new TThreadInput(true);
ThreadInput->Resume();
//End
}
//--------------------------------------------------------------------------- void __fastcall TForm1::InputDirectoryChange()
{
TSearchRec sr;
DWORD result; result=MsgWaitForMultipleObjects(1,&inputhandle,false,INFINITE,QS_ALLINPUT);
Sleep(2);
if(result==WAIT_OBJECT_0)
{
if(FindFirst(CurrentDir "\\input\\*.agn",faAnyFile,sr)==0)
{
do{
AnsiString agenttext="";
char buf[512];
memset(&buf,0x00,sizeof(buf));
TMemoryStream *filestream=new TMemoryStream();
filestream->LoadFromFile(CurrentDir "\\input\\" sr.Name);
memcpy(buf,filestream->Memory,filestream->Size);
delete filestream;
agenttext=buf;
Memo1->Text=agenttext;
}while(FindNext(sr)==0);
FindNextChangeNotification(inputhandle);
}
}
/*else
{
Application->ProcessMessages();
} */
}
//-----------------------------------------------------------------------------
Thread的程式如下
=====================================================================
//--------------------------------------------------------------------------- #include
#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 TThreadInput::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//--------------------------------------------------------------------------- __fastcall TThreadInput::TThreadInput(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TThreadInput::Execute()
{
while(!Terminated)
{
Form1->InputDirectoryChange();
}
//---- Place thread code here ----
}
//---------------------------------------------------------------------------