To : errorcode<br>我现在按你说‘将文件的打开,关闭放到外循环中’还是消耗很高。<br>下面是我整个线程的代码:<br>//*********************************<br>// 处理请求的历史数据<br>// Ansili 2006-10-10<br>//********************************<br>unit unitProcReceiveHistoryThread;<br><br>interface<br><br>uses<br> Classes, Windows, SysUtils, Contnrs, SyncObjs,<br> unitDataModule, unitGlobalStructure;<br><br>type<br> ProcReceiveHistoryThread = class(TThread)<br> private<br> Port : Word;<br> DataPath : String;<br> { Request History Data }<br> RequestQueue : TQueue;<br> RequestQueueEvent : TEvent;<br> RequestQueueSection : TCriticalSection;<br><br> SaveFileName : String;<br> SaveFileHandle : Integer;<br> SaveFileStream : TFileStream;<br> <br> procedure FreeLocalVariant(Sender : TObject);<br> procedure proBuffer(const ALength : Integer; const ABuffer : PChar);<br> procedure proReceiveHisData(AMsgRecieveHisData: MyBodyReceiveHisData;<br> ABuffer : PChar;ALength : integer);<br> procedure proSaveHistoryFile(AMsgRecieveHisData: MyBodyReceiveHisData;<br> ABuffer : PChar;ALength : integer);<br> protected<br> procedure Execute; override;<br> public<br> constructor Create(ACreateSuspended: Boolean = True); virtual;<br> procedure SetRequestQueue(AQueue: TQueue);<br> procedure SetRequestQueueEvent(AQueueEvent: TEvent);<br> procedure SetRequestQueueSection(AQueueSection: TCriticalSection);<br> procedure SetPort(APort : Word);<br> function GetThreadStatus : Boolean;<br> end;<br><br>implementation<br><br>uses unitGlobalSet;<br><br>{ ProcReceiveHistoryThread }<br><br>constructor ProcReceiveHistoryThread.Create(ACreateSuspended: Boolean);<br>begin<br> inherited Create(ACreateSuspended);<br> OnTerminate := FreeLocalVariant;<br> FreeOnTerminate := true;<br><br> SaveFileHandle := -1;<br> SaveFileStream := nil;<br> DataPath := GlobalWorkInfo.DataPath;<br>end;<br><br>procedure ProcReceiveHistoryThread.FreeLocalVariant(Sender: TObject);<br>begin<br> if SaveFileStream <> nil then<br> begin<br> FreeAndNil(SaveFileStream);<br> end;<br> AppendLog('终止历史数据保存线程 ProcReceiveHistoryThread');<br>end;<br><br>function ProcReceiveHistoryThread.GetThreadStatus: Boolean;<br>begin<br> Result := Self.Terminated;<br>end;<br><br>procedure ProcReceiveHistoryThread.SetPort(APort: Word);<br>begin<br> Port := APort;<br>end;<br><br>procedure ProcReceiveHistoryThread.SetRequestQueue(AQueue: TQueue);<br>begin<br> RequestQueue := AQueue;<br>end;<br><br>procedure ProcReceiveHistoryThread.SetRequestQueueEvent(<br> AQueueEvent: TEvent);<br>begin<br> RequestQueueEvent := AQueueEvent;<br>end;<br><br>procedure ProcReceiveHistoryThread.SetRequestQueueSection(<br> AQueueSection: TCriticalSection);<br>begin<br> RequestQueueSection := AQueueSection; <br>end;<br><br>procedure ProcReceiveHistoryThread.Execute;<br>var<br> DataBuf : TMyDataBuffer;<br>begin<br> FreeOnTerminate := true;<br> while(not Terminated)do<br> begin<br> if(RequestQueue.Count <= 0) then<br> begin<br> RequestQueueEvent.ResetEvent;<br> RequestQueueEvent.WaitFor(INFINITE);<br> end<br> else<br> begin<br> DataBuf := nil;<br> RequestQueueSection.Enter;<br> try<br> DataBuf := TMyDataBuffer(RequestQueue.Pop);<br> except on Ex : Exception do<br> AppendLog(IntToStr(Port) + ': ProcReceiveHistoryThread Pop Queue Error : ' + Ex.Message);<br> end;<br> RequestQueueSection.Leave;<br> if DataBuf <> nil then<br> begin<br> try<br> proBuffer(DataBuf.BufferLength,DataBuf.BufferData);<br> except on Ex : Exception do<br> AppendLog(IntToStr(Port) + ': ProcReceiveHistoryThread DoBuffer Error : ' + Ex.Message);<br> end;<br> try<br> FreeAndNil(DataBuf);<br> except on Ex : Exception do<br> AppendLog(IntToStr(Port) + ': ProcReceiveHistoryThread,FreeAndNil(DataBuf) Error : ' + Ex.Message);<br> end;<br> end;<br> end;<br> end;<br>end;<br><br>procedure ProcReceiveHistoryThread.proBuffer(const ALength: Integer;<br> const ABuffer: PChar);<br>var<br> i : integer;<br> tmpLen, len : integer;<br> ErrorStr : String;<br> DataFlag : Cardinal;<br> MsgHeader : MyMessageHeader; //消息头<br> MsgRecieveHisData: MyBodyReceiveHisData; //接收请求的历史数据<br> DataBuffer : PChar;<br>begin<br> i := 0; //指针位置<br> if i + SizeOf(Cardinal) > ALength then<br> begin<br> Exit;<br> end;<br> tmpLen := 0;<br> DataFlag := 0;<br> Move(ABuffer,DataFlag ,SizeOf(Cardinal));<br> if DataFlag <> MY_MESSAGE_FLAG then //判断标志位<br> begin<br> Exit;<br> end;<br> i := i + SizeOf(Cardinal);<br> //读取消息头<br> if i + SizeOf(MyMessageHeader) > ALength then<br> begin<br> Exit;<br> end;<br> Move(ABuffer,MsgHeader,SizeOf(MyMessageHeader));<br> i := i + SizeOf(MyMessageHeader);<br> case MsgHeader.message_type of<br> 204 : if (i + SizeOf(MyBodyReceiveHisData)) < ALength then<br> begin<br> Move(ABuffer,MsgRecieveHisData,SizeOf(MyBodyReceiveHisData));<br> i := i + SizeOf(MyBodyReceiveHisData);<br> len := MsgRecieveHisData.length;<br> if ((i + len) = ALength) and (len > 0) then<br> begin<br> try<br> DataBuffer := AllocMem(len);<br> try<br> Move(ABuffer,DataBuffer[0],len);<br> i := i + len;<br> //proReceiveHisData(MsgRecieveHisData,DataBuffer,len);<br> proSaveHistoryFile(MsgRecieveHisData,DataBuffer,len);<br> except on Ex : Exception do<br> AppendLog(IntToStr (Port) + ': ProcReceiveHistoryThread,DoDataTransfer Error : ' + Ex.Message);<br> end;<br> finally<br> FreeMem(DataBuffer);<br> end;<br> end;<br> end;<br> end;<br>end;<br><br>procedure ProcReceiveHistoryThread.proReceiveHisData(<br> AMsgRecieveHisData: MyBodyReceiveHisData; ABuffer: PChar;<br> ALength: integer);<br>var<br> filename : String;<br> FileHandle : Integer;<br> curLength : Cardinal;<br> msgRecvHisData : MyReceiveHistoryData;<br>begin<br> filename := DataPath + AMsgRecieveHisData.file_name;<br> FillChar(msgRecvHisData,SizeOf(MyReceiveHistoryData),0);<br> if FileExists(filename) then<br> begin<br> try<br> FileHandle := FileOpen(filename,fmOpenReadWrite or fmShareDenyWrite);<br> FileRead(FileHandle,msgRecvHisData,SizeOf(MyReceiveHistoryData));<br> if msgRecvHisData.current_length < msgRecvHisData.total_length then<br> begin<br> msgRecvHisData.total_length := AMsgRecieveHisData.total_length;<br> msgRecvHisData.current_length := msgRecvHisData.current_length + SizeOf(Cardinal) + ALength;<br> curLength := FileSeek(FileHandle,0,0);<br> FileWrite(FileHandle,msgRecvHisData,SizeOf(MyReceiveHistoryData));<br> //写数据部分<br> curLength := FileSeek(FileHandle,0,2);<br> FileWrite(FileHandle,ALength,SizeOf(integer));<br> FileWrite(FileHandle,ABuffer[0],ALength);<br> end;<br> finally<br> FileClose(FileHandle);<br> end;<br> end;<br>end;<br><br>procedure ProcReceiveHistoryThread.proSaveHistoryFile(<br> AMsgRecieveHisData: MyBodyReceiveHisData; ABuffer: PChar;<br> ALength: integer);<br>var<br> filename : String;<br> FileHandle : Integer;<br> curLength : Cardinal;<br> msgRecvHisData : MyReceiveHistoryData;<br>begin<br> filename := DataPath + AMsgRecieveHisData.file_name;<br> if SaveFileName <> filename then<br> begin<br> if SaveFileStream <> nil then<br> begin<br> FreeAndNil(SaveFileStream);<br> end;<br> SaveFileName := DataPath + AMsgRecieveHisData.file_name;<br> SaveFileStream := TFileStream.Create(SaveFileName,fmOpenReadWrite or fmShareDenyWrite);<br> end;<br> FillChar(msgRecvHisData,SizeOf(MyReceiveHistoryData),0);<br> if FileExists(SaveFileName)and(SaveFileStream <> nil) then<br> begin<br> SaveFileStream.Position := 0;<br> SaveFileStream.ReadBuffer(msgRecvHisData,SizeOf(MyReceiveHistoryData));<br> if msgRecvHisData.current_length < msgRecvHisData.total_length then<br> begin<br> msgRecvHisData.total_length := AMsgRecieveHisData.total_length;<br> msgRecvHisData.current_length := msgRecvHisData.current_length + SizeOf(Cardinal) + ALength;<br> SaveFileStream.Position := 0;<br> SaveFileStream.WriteBuffer(msgRecvHisData,SizeOf(MyReceiveHistoryData));<br> //写数据部分<br> SaveFileStream.Position := SaveFileStream.Size;<br> SaveFileStream.WriteBuffer(ALength,SizeOf(integer));<br> SaveFileStream.WriteBuffer(ABuffer[0],ALength);<br> end;<br> end;<br>end;<br><br>end.