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

請問有關Thread的問題

缺席
willchen
初階會員


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

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-22 01:55:45 IP:211.74.xxx.xxx 未訂閱
我使用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 ----
}
//---------------------------------------------------------------------------
willchen
初階會員


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-22 05:04:35 IP:211.74.xxx.xxx 未訂閱
真是不好意思,抓Bug抓了一堆,終於把程式搞定了,真是有點睡不著呀(這問題困擾我很久了),看來離畢業越來越接近了,每次承蒙各位先進指教,感激不盡 我將正確的程式 > 以下是我用多執行緒監看子目錄,一但子目錄下有檔案,即使用 class="code"> //--------------------------------------------------------------------------- #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_LAST_WRITE); //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); if(result==WAIT_OBJECT_0) { int FileFound=FindFirst(CurrentDir "\\input\\*.agn",faAnyFile,sr); while(FileFound==0) { AnsiString agenttext=""; char buf[1024]; memset(&buf,0x00,sizeof(buf)); TMemoryStream *filestream=new TMemoryStream(); filestream->LoadFromFile(CurrentDir "\\input\\" sr.Name); //char buf[filestream->Size]; memset(&buf,0x00,sizeof(buf)); memcpy(buf,filestream->Memory,filestream->Size); //Sleep(2); agenttext=buf; Memo1->Text=agenttext; delete filestream; DeleteFile((CurrentDir "\\input\\" sr.Name).c_str()); FileFound=FindNext(sr); } FindNextChangeNotification(inputhandle); } else { Application->ProcessMessages(); } } //----------------------------------------------------------------------------- 多執行緒部分 =====================================================================
//---------------------------------------------------------------------------    #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)
   {
    Synchronize(Loop);
   }
        //---- Place thread code here ----
}
//---------------------------------------------------------------------------
void __fastcall TThreadInput::Loop()
{
 Form1->InputDirectoryChange();
} 
系統時間:2024-05-08 9:46:32
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!