Ẹửa moọt nhaứ ủaừ heỏt hán cho thuẽ về danh saựch caực nhaứ coự theồ cho thuẽ

Một phần của tài liệu THREAD trong DELPHI.doc (Trang 32 - 36)

3. 2C ÁC VẤN ẹỀ CHÍNH CẦN GIẢI QUYẾT

3.3.6.2ẹửa moọt nhaứ ủaừ heỏt hán cho thuẽ về danh saựch caực nhaứ coự theồ cho thuẽ

ẹeồ ủửa moọt nhaứ ủaừ heỏt hán cho thuẽ về danh saựch caực nhaứ coự theồ cho thuẽ, bán haừy nhaỏn nuựt Heỏt Thuẽ Nhaứ, moọt khung trao ủoồi Confirm hieọn ra nhử dửụựi ủãy:

Giaỷi thuaọt cho vieọc ủửa moọt nhaứ heỏt hán thuẽ nhaứ về danh saựch nhaứ coự theồ cho thuẽ

Sinh ra moọt thread tẽn TUnRentThread: procedure TUnRentThread.Execute; begin

EnterCriticalSection(CritSect); // Vaứo vuứng tranh chaỏp

Synchronize(DoThread);

LeaveCriticalSection(CritSect); // Ra khoỷi vuứng tranh chaỏp

end;

DoThread thửùc hieọn nhử sau:

procedure TUnRentThread.DoThread; var i, count : integer; fHome : THomeInfo; begin if ChatForm.HomeData.HomeUnRent(FIndex) then // Neỏu gói haứm HomeUnRent thaứnh cõng

begin

Count := ChatForm.homeData.HomeGetCount; // Count laứ toồng soỏ nhaứ coự theồ cho thuẽ hay baựn

for i := 0 to ChatForm.ServerSocket.Socket.ActiveConnections - 1 do

ChatForm.SendMessageToClient(ChatForm.ServerSocket.Socket.Connections[i], idSetVLBNumItems, Count, fHome);

// Gửỷi thõng ủieọp ủeỏn toaứn boọ client yẽu cầu caọp nhaọt lái danh saựch caực nhaứ cho thuẽ hoaởc baựn

end; end;

Noọi dung cuỷa haứm HomeUnRent:

function THomeData.HomeUnRent(delindex : longint) : boolean; (* ẹửa vaứo

delindex : vũ trớ cần xoựa khoỷi danh saựch nhaứ ủaừ cho thuẽ Cho ra

Result = true neỏu thaứnh cõng Result = false neỏu thaỏt bái *)

var

buf : pchar;

fHome : THomeInfo; index, count : longint; trent: TIndexFile; begin

seek(rentfile, delindex); read(rentfile, index);

//Laỏy index laứ vũ trớ cuỷa record chửựa thõng tin về nhaứ trong datafile tửứ rentfile. seek(datafile, index);

read(datafile, fHome);

// Laỏy thõng tin nhaứ vaứo bieỏn fHome HomeInsert(fHome, index);

// Thẽm thõng tin nhaứ naứy vaứo danh saựch nhaứ coự theồ cho thuẽ hoaởc baựn

// Caực leọnh sau ủãy ủeồ caột 4 byte ụỷ vũ trớ delindex khoỷi rentfile (sau khi laứm xong rentfile

seừ giaỷm ủi 4 bytes.

assignfile(trent, pathname + '1z2y3x4w.qgb'); rewrite(trent);

// Táo ra moọt file trent tám thụứi count := delindex;

if count > 0 then

// Cheựp moọt block tửứ ủầu file rentfile ủeỏn delindex – 1 vaứo trent begin

getmem(buf, count*idxtypeSize); seek(rentfile, 0);

blockread(rentfile, buf^, count); blockwrite(trent, buf^, count); freemem(buf, count*idxtypesize); end;

count := filesize(rentfile) - count - 1; if count > 0 then

// Cheựp moọt khoỏi tửứ vũ trớ delindex + 1 trong rentfile vaứo trent begin

getmem(buf, count*idxtypesize); seek(rentfile, delindex + 1); blockread(rentfile, buf^, count); blockwrite(trent, buf^, count); freemem(buf, count*idxTypesize); end;

result := IOResult = 0; if result then

// Neỏu khõng xaỷy ra loĩi, ủoựng 2 file rentfile vaứ trent; xoựa rentfile; ủoồi tẽn trent thaứnh rentfile

rồi mụỷ lái rentfile begin CloseFile(trent); CloseFile(rentfile); Erase(rentfile); Rename(trent, rentfilename); AssignFile(rentfile, rentfilename); Reset(rentfile); end; result := IOresult = 0; end;

Một phần của tài liệu THREAD trong DELPHI.doc (Trang 32 - 36)