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

請問一個利用多緒存檔的問題

尚未結案
sue690226
一般會員


發表:23
回覆:15
積分:7
註冊:2004-07-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-30 13:26:33 IP:61.222.xxx.xxx 未訂閱
這是我的一個多緒程式請問各位高手為何我可以通過Compiler但開啟這一個多序卻會出現錯誤呢?且錯誤都是出現在存檔的那一行。
#include 
#pragma hdrstop
#include "Main.h"
#include "Login.h"
#include "ReceiveData.h"
#include "Monitor.h"
#include "stdio.h"
#pragma package(smart_init)
FILE *TFile,*ECGFile,*SPO2RFile,*SPO2GFile;
//---------------------------------------------------------------------------    //   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 TReceiveData::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------    __fastcall TReceiveData::TReceiveData(bool CreateSuspended)
        : TThread(CreateSuspended)
{
 FreeOnTerminate=true;
}
//---------------------------------------------------------------------------
void __fastcall TReceiveData::Execute()
{
        //---- Place thread code here ----
        while(1)
        {
        Synchronize(File_Store);
        Synchronize(Receive);
        Sleep(1);
        }
}
//---------------------------------------------------------------------------
void _fastcall TReceiveData::Receive()
{        
 if (Count == 2000)
     {
       MonitorForm->ECGChart->Series[0]->Clear();
       MonitorForm->TemperatureChart->Series[0]->Clear();
       MonitorForm->SPO2Chart->Series[0]->Clear();
       MonitorForm->SPO2Chart->Series[1]->Clear();
       Count = 0;
     }
     int Channel;
   ClearCommError(MainForm->hComm1,&dwError,&cs);
   if (cs.cbInQue > sizeof(inbuff))
   {
     PurgeComm(MainForm->hComm1, PURGE_RXCLEAR);
     return;
    }
   if(cs.cbInQue>100)
   {
   ReadFile(MainForm->hComm1,inbuff,100,&nBytesRead,NULL);       if(inbuff[0]>=64)
   {
   for(int j=0 ; j<100 ;j=j 2)
   {
    Buff1=inbuff[j 1] ((inbuff[j]&7)*64);
    Channel=(inbuff[j]>>3)& 7;
    if (Channel == 0)
    {
     MonitorForm->ECGChart->Series[0]->AddXY(Count,Buff1,"",clTeeColor);
     //fprintf(ECGFile,"%d",Buff1);
    }
    if(Channel==1)
    {
     MonitorForm->TemperatureChart->Series[0]->AddXY(Count,(MainForm->Temperature[Buff1]/100),"",clTeeColor);
     //fprintf(TFile,"%d",Buff1);
    }
    if (Channel == 2)
    {
     MonitorForm->SPO2Chart->Series[0]->AddXY(Count,Buff1,"",clTeeColor);
     //fprintf(SPO2RFile,"%d",Buff1);
    }
    if (Channel == 3)
    {
     MonitorForm->SPO2Chart->Series[1]->AddXY(Count,Buff1,"",clTeeColor);
     //fprintf(SPO2GFile,"%d",Buff1);
    }
    Count  ;
   }
   }
   else
   {
    for(int j=1 ; j<100 ;j=j 2)
   {        if (Channel == 0)
    {
     MonitorForm->ECGChart->Series[0]->AddXY(Count,Buff1,"",clTeeColor);
     //fprintf(ECGFile,"%d",Buff1);
    }
    if(Channel==1)
    {
     MonitorForm->TemperatureChart->Series[0]->AddXY(Count,(MainForm->Temperature[Buff1]/100)*2,"",clTeeColor);
     //fprintf(TFile,"%d",Buff1);
    }
    if (Channel == 2)
    {
     MonitorForm->SPO2Chart->Series[0]->AddXY(Count,Buff1,"",clTeeColor);
     //fprintf(SPO2RFile,"%d",Buff1);
    }
    if (Channel == 3)
    {
     MonitorForm->SPO2Chart->Series[1]->AddXY(Count,Buff1,"",clTeeColor);
     //fprintf(SPO2GFile,"%d",Buff1);
    }
    Count  ;
    }
    ReadFile(MainForm->hComm1,inbuff1,1,&nBytesRead,NULL);
   }
  }
}
//-----------------------------------------------------------------------------------------------------------------------------------------
void _fastcall TReceiveData::File_Store()
{
    AnsiString Time1,Date1,Temp;
   Date1=Date().FormatString("YYYYMMDD");
   Time1=Time().FormatString("HHMMSS");
   AnsiString ID=LoginForm->IDEdit->Text "_" Date1 "_" Time1;
   AnsiString TFileName,EFileName,SRFileName,SGFileName;        TFileName="C:\\Project\\File_Store\\" ID "Tem.Tps";
    TFile=fopen(TFileName.c_str(),"w ");
    EFileName="C:\\Project\\File_Store\\" ID "ECG.Eps";
    ECGFile=fopen(EFileName.c_str(),"w ");
    SRFileName="C:\\Project\\File_Store\\" ID "SPO2R.Sps";
    SPO2RFile=fopen(SRFileName.c_str(),"w ");
    SGFileName="C:\\Project\\File_Store\\" ID "SPO2G.Sps";
    SPO2GFile=fopen(SGFileName.c_str(),"w ");    }
lu
高階會員


發表:11
回覆:189
積分:195
註冊:2003-11-19

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-30 13:52:48 IP:221.169.xxx.xxx 未訂閱
看了一下你的程式,有個疑問,你這THREAD是不是在整個程式只執行一次,還是會產生好多個?如下例:
TReceiveData *ReceiveData[3];
for (int c=0;c<3;c  )
  ReceiveData[c] = new TReceiveData(this);
如果是這樣,你的程式馬會出現一個大漏洞喔,那就是你把 FILE *TFile,*ECGFile,*SPO2RFile,*SPO2GFile; 宣告成整體變數,這樣變成所有的 OBJECT 都使用這些變數 建議你將上述這些變數宣告成 private 試試看吧,希望對你有幫助 ========================= 大家一起快樂寫程式
ddy
站務副站長


發表:262
回覆:2105
積分:1169
註冊:2002-07-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-30 17:34:11 IP:202.145.xxx.xxx 未訂閱
張貼程式請加上縮排 【公告】程式碼張貼規則與方法 已協助修改,下次張貼程式碼時請配合
系統時間:2024-06-02 9:58:02
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!