全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1312
推到 Plurk!
推到 Facebook!

Multi-thread監控子目錄的問題

 
willchen
初階會員


發表:64
回覆:81
積分:29
註冊:2003-05-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-24 04:17:36 IP:211.74.xxx.xxx 未訂閱
我使用一個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="<Header><ID>MACE" IPConvert(LocalIP) "10
</Header><ServiceItem>" fullscript "";     //紀錄第一個host IP
 AnsiString FirstItem=fullscript.SubString(fullscript.Pos("") 7,fullscript.Pos("")-(fullscript.Pos("") 7));
 FirstIP=FirstItem.SubString(FirstItem.Pos("") 4,FirstItem.Pos("")-(FirstItem.Pos("") 4));
 //End     DS_launcher_client->Host=IPConverter(FirstIP); //設定DS_launcher_client的IP
 DS_launcher_client->Active=True;    //DS_launcher_client開啟     tempstream->Clear();     //再把fullscript寫入成ID.agn
 memset(buf,0x00,sizeof(buf));
 tempstream->SetSize(fullscript.Length());
 memcpy(tempstream->Memory,fullscript.c_str(),fullscript.Length());
 tempstream->SaveToFile(CurrentDir "\\OK\\MACE" IPConvert(LocalIP) ".plain");     DSTDES->SetKey(::TKey128);
 DSTDES->GenerateRandomKey();
 delete tempstream;
 DSTDES->EncryptFileA(CurrentDir "\\OK\\MACE" IPConvert(LocalIP) ".plain",CurrentDir "\\OK\\MACE" IPConvert(LocalIP) ".agn");
 Sleep(100);
 /*delete tempstream;
 DeleteFile(CurrentDir "\\OK\\MACE" IPConvert(LocalIP) ".plain"); */
 /*TMemoryStream *filestream=new TMemoryStream();
 filestream->LoadFromFile(CurrentDir "\\OK\\MACE" IPConvert(LocalIP) ".agn");
 DS_launcher_client->Socket->SendStream(filestream); */
} 
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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-24 04:36:36 IP:211.74.xxx.xxx 未訂閱
抱歉,上傳檔案太大,此篇可刪除
系統時間:2024-05-18 13:07:07
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!