Multi-thread監控子目錄的問題 |
|
willchen
初階會員 發表:64 回覆:81 積分:29 註冊:2003-05-23 發送簡訊給我 |
我使用一個multithread去監控一個目錄,一但有檔案(文字檔)在此子目錄下,即讀取傳送到Server,但我發現我Client端在存檔,與Multithread在監控下,會有Accesse violation的錯誤產生(我猜可能一個要存一個要讀所以產生問題),但我不知道要怎麼避免,煩請各位高手可以提供解決方案,感激不盡,由於上傳執行檔沒有用,所以只能貼程式,我把可能發生問題的那些部分貼出來
PS:附帶完整程式 存入該監控子目錄的副程式(倒數第二行存檔,DSTDES->EncryptFileA(.....),因為使用加密程式,重點在存檔那邊,其他應該沒有問題
=====================================================================
void _fastcall TForm1::DS_Packer(void) //Directory Service專用的Agent Packer(把agent包裝,並且把agent外層加密) { // 先從agent.tmp讀檔寫入header char buf[512]; AnsiString fullscript=""; TMemoryStream *tempstream=new TMemoryStream(); tempstream->LoadFromFile(CurrentDir "\\agent.tmp"); memset(&buf,0x00,sizeof(buf)); memcpy(buf,tempstream->Memory,tempstream->Size); fullscript=buf; fullscript="MultiThread監控子目錄的副程式 ==================================================================== void __fastcall TForm1::OK_DIR_Change() { DWORD result; result=MsgWaitForMultipleObjects(1,&OK_Dir_Handle,false,INFINITE,QS_ALLINPUT); if(result==WAIT_OBJECT_0) { //DS_Launcher(); TSearchRec sr; int FileFound=FindFirst(OKpath "\\*.agn",faAnyFile,sr); while(FileFound==0) { //處理資料 TMemoryStream *filestream=new TMemoryStream(); filestream->LoadFromFile(OKpath sr.Name); //TFileStream *filestream=new TFileStream(OKpath sr.Name,fmOpenReadWrite); //DS_launcher_client->Open(); int i=DS_launcher_client->Socket->SendStream(filestream); Sleep(1000); // //找下一個檔案 Sleep(500); delete filestream; DeleteFile(OKpath sr.Name); FileFound=FindNext(sr); } FindNextChangeNotification(OK_Dir_Handle); } else { Application->ProcessMessages(); } }MultiThread程式部分 ===================================================================== #pragma hdrstop #include "Unit1.h" #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 TThreadOKInput::UpdateCaption() // { // Form1->Caption = "Updated in a thread"; // } //--------------------------------------------------------------------------- __fastcall TThreadOKInput::TThreadOKInput(bool CreateSuspended) : TThread(CreateSuspended) { } //--------------------------------------------------------------------------- void __fastcall TThreadOKInput::Execute() { while (!Terminated) { Synchronize(Loop); Sleep(0); } //---- Place thread code here ---- } //--------------------------------------------------------------------------- void __fastcall TThreadOKInput::Loop() { Form1->OK_DIR_Change(); }小弟我查了很久都查不到什麼原因,不過我猜可能是一個在存檔,一個再監看時 發生access violation的問題
附加檔案:37866_論文實作.zip
|
willchen
初階會員 發表:64 回覆:81 積分:29 註冊:2003-05-23 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |