Tìm hiểu về thread trong delphi

1 Giới thiệu Thread trong Delphi 3

1.1 Thread là gì? 3

1.2 Ứng dụng của Thread 3

1.3 Sử dụng TThread trong Delphi 4

1.3.1 Các method đáng chú ý khi viết Thread 5

1.3.2 Các property đáng chú ý khi viết Thread 6

1.3.3 Tranh chấp dữ liệu khi viết multi-thread 7

1.3.4 Sử Dụng Critical Section trong chương trình 9

2 Giao tiếp giữa các máy mô hình Client/Server 9

2.1 TClientSocket 10

2.1.1 Ghi dữ liệu đến server 10

2.1.2 Đọc dữ liệu từ server 11

2.2 TClientWinSocket 11

2.3 TserverSocket 12

2.3.1 Truyền dữ liệu đến một Client 13

2.3.2 Đọc dữ liệu từ một client 13

2.3.3 Bạn có thể biết chính xác Client thứ mấy đã gửi dữ liệu đến server socket 14

2.4 TServerWinSocket 14

2.4.1 Lắng nghe yêu cầu kết nối của Client 14

2.4.2 Quản lý dãy các client socket đã kết nối với server socket 15

2.5 TServerClientWinSocket 15

3 Giới thiệu chương trình 16

3.1 Nội dung và ý nghĩa chương trình 16

3.2 Các vấn đề chính cần giải quyết 20

3.2.1 Tổ chức dữ liệu trong chương trình 20

3.2.2 Kết nối cho client 22

3.2.3 Hủy kết nối cho client 23

3.2.4 Tranh chấp dữ liệu 23

3.2.5 Dùng thread để phân chia CPU cho các thao tác của client 23

3.3 Các chức năng cho người quản lý 24

3.3.1 Tìm kiếm một nhà 25

3.3.2 Lấy thông tin của một nhà 26

3.3.3 Thêm thông tin một nhà mới 27

3.3.4 Xóa thông tin một nhà 29

3.3.5 Cập nhật thông tin một nhà 30

3.3.6 Thống kê số nhà đã cho thuê 32

3.3.6.1 Đếm số nhà đã cho thuê 32

3.3.6.2 Đưa một nhà đã hết hạn cho thuê về danh sách các nhà có thể cho thuê 33

3.3.7 Thống kê số nhà đã bán 37

3.3.7.1 Đếm số nhà đã bán 37

3.4 Các chức năng cho người cập nhật thông tin nhà 38

3.4.1 Tìm kiếm một nhà 39

3.4.2 Lấy thông tin của một nhà 39

3.4.3 Cập nhật thông tin một nhà 39

3.4.4 Thêm thông tin một nhà mới 40

3.4.5 Xóa thông tin một nhà 40

3.5 Các chức năng cho một khách hàng 41

3.5.1 Lấy thông tin về một nhà 41

3.5.2 Tìm kiếm một nhà 41

3.5.3 Tìm kiếm theo điều kiện 43

3.5.4 Đăng ký thuê nhà 48

3.5.5 Đăng ký mua nhà 52

3.6 Mã chương trình kinh doanh nhà đất trên mạng client/server 55

3.6.1 Nha.dpr 55

3.6.2 Nhaf.pas 56

3.6.3 ConectDlg.pas 78

3.6.4 Findf.pas 81

3.6.5 HomeInfo.pas 85

3.6.6 HomeTools.pas 88

3.6.7 Informf.pas 105

3.6.8 InputThuef.pas 107

3.6.9 MytdRTF.pas 109

3.6.10 MyThread.pas 113

3.6.11 RepThuef.pas 129

 

 

doc130 trang | Chia sẻ: huong.duong | Lượt xem: 1370 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Tìm hiểu về thread trong delphi, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
ịnh mua căn nhà đã chọn. Thao tác trong chương trình để cho mua một căn nhà làm như sau: Chương trình client sẽ gửi một thông điệp đến server yêu cầu mua nhà kèm theo vị trí của nhà trong danh sách nhà. Khi nhận được thông điệp thuê nhà này, chương trình server sẽ tạo ra một thread tên TMuaNhaThread: procedure TMuaNhaThread.Execute; begin EnterCriticalSection(CritSect); Synchronize(DoThread); LeaveCriticalSection(CritSect); end; DoThread trong mua nhà chủ yếu gọi lệnh sau: if HomeData.HomeSell(index) then ServerUpdateVLB Ý nghĩa của hàm HomSell như sau: function THomeData.HomeSell(index : longint) : boolean; var count, dataIndex : longint; buf : pchar; ftidx : TIndexfile; begin result := false; if index < 0 then exit; seek(indexfile, index); read(indexfile, dataindex); // dataindex là vị trí của thông tin nhà trong datafile seek(sellfile, filesize(sellfile)); write(sellfile, dataindex); // Ghi 4 byte của dataindex vào cuối sellfile // Các lệnh sau đây để xóa 4 byte tại vị trí index trong indexfile. Sau khi thực hiện, indexfile giảm 4 byte assignfile(ftidx, pathname + '1z2y3x4w.qgb'); rewrite(ftidx); count := index; if count > 0 then begin getmem(buf, count*idxtypeSize); seek(IndexFile, 0); blockread(IndexFile, buf^, count); blockwrite(ftIdx, buf^, count); freemem(buf, count*idxtypeSize); end; count := filesize(IndexFile)- count - 1; if count > 0 then begin getmem(buf, count*idxtypeSize); seek(IndexFile, index + 1); blockread(IndexFile, buf^, count); blockwrite(ftidx, buf^, count); freemem(buf, count*idxtypeSize); end; result := IOResult = 0; if result then begin CloseFile(ftIdx); CloseFile(IndexFile); Erase(IndexFile); Rename(ftIdx, indexfilename); AssignFile(IndexFile, indexfilename); Reset(IndexFile); end; end; Dĩ nhiên thông tin về căn nhà vừa bán sẽ không bị mất đi. Người quản lý vẫn có thể xem thông tin về các nhà đã bán. Mã chương trình kinh doanh nhà đất trên mạng client/server Nha.dpr Nha.dpr là chương trình chính. (* nha.dpr Written by Le Tuan *) program nha; uses Graphics, Controls, Forms, nhaf in 'nhaf.pas' {ChatForm}, conectdlg in 'conectdlg.pas' {ConnectDlg}, myThread in 'myThread.pas', homeInfo in 'homeInfo.pas' {HomeInfoForm}, hometools in 'hometools.pas', findf in 'Findf.pas' {FindForm}, mytdrtf in 'mytdrtf.pas', repthuef in 'repthuef.pas' {RepThueForm}, inputthuef in 'inputthuef.pas' {InputThue}, informf in 'informf.pas' {InfoForm}; {$R *.RES} begin ConnectDlg := TConnectDlg.Create(Application); with ConnectDlg do begin if ShowModal = mrOK then begin ChatForm := TChatForm.Create(application); ChatForm.ShowModal; ChatForm.Release; end; Release; end; end. Nhaf.pas Nhaf.pas chứa khung trao đổi chính của chương trình. Khung trao đổi này dùng chung cho cả 3 đối tượng người sử dụng, tuy nhiên cũng có một số khác biệt nhỏ. (* nhaf.pas Written by Le Tuan *) unit nhaf; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls, Buttons, ScktComp, ExtCtrls, ComCtrls, VLB, homeInfo, HomeTools, MyThread, Findf, Tranbtn, ShadowForm, mytdRTF, myDialog, inputthuef, QFButton, InformF; const WM_ChangeIndex = WM_USER + 5000; type TChatForm = class(TShadowForm) Image1: TImage; VLB: TVLB; TextSearch: TEdit; Label1: TLabel; Bevel1: TBevel; RichEdit1: TRichEdit; Bevel2: TBevel; Bevel3: TBevel; AdminLTBtnAdd: TLTBtn; AdminLTBtnModify: TLTBtn; AdminLTBtnDel: TLTBtn; DicLTBtnModify: TLTBtn; DicLTBtnDel: TLTBtn; DicLTBtnAdd: TLTBtn; Bevel4: TBevel; ClientSocket: TClientSocket; ServerSocket: TServerSocket; CusLTBtnFind: TLTBtn; CusLTBtnThue: TLTBtn; CusLTBtnBan: TLTBtn; AdminLTBtnRepThue: TLTBtn; Image2: TImage; AdminLTBtnRepBan: TLTBtn; Image3: TImage; QFButton1: TQFButton; Image4: TImage; LabelName: TLabel; procedure FormPaint(Sender: TObject); procedure FormCreate(Sender: TObject); procedure VLBGetItem(Sender: TObject; Index: Integer; var ItemString: String); procedure VLBClick(Sender: TObject); procedure ClientSocketConnect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket); procedure ServerSocketClientRead(Sender: TObject; Socket: TCustomWinSocket); procedure CusLTBtnFindClick(Sender: TObject); procedure CusLTBtnThueClick(Sender: TObject); procedure CusLTBtnBanClick(Sender: TObject); procedure DicLTBtnModifyClick(Sender: TObject); procedure DicLTBtnDelClick(Sender: TObject); procedure DicLTBtnAddClick(Sender: TObject); procedure AdminLTBtnAddClick(Sender: TObject); procedure AdminLTBtnDelClick(Sender: TObject); procedure AdminLTBtnModifyClick(Sender: TObject); procedure TextSearchChange(Sender: TObject); procedure TextSearchKeyPress(Sender: TObject; var Key: Char); procedure AdminLTBtnRepThueClick(Sender: TObject); procedure AdminLTBtnRepBanClick(Sender: TObject); procedure QFButton1Click(Sender: TObject); procedure FormActivate(Sender: TObject); private { Private declarations } procedure wmNcHitTest(var Msg : TwmNcHitTest); message wm_NcHitTest; function TestTLTBtn(p : TPoint) : boolean; procedure DrawForm(Level : integer); private function FindClient(ClientSocket : TCustomWinSocket) : integer; procedure ShowData; procedure ConnectAdmin; procedure ConnectModifier; procedure ConnectCustomer; function SetHomeInfo(hasDefault : boolean) : boolean; procedure ServerUpdateVLB; procedure FileListenItemClick(Sender: TObject); public homeData : THomeData; fHome, ffhome : THomeInfo; fIndex : longint; procedure InsertData(Home : THomeInfo); procedure ModifyData(index : longint; Home : THomeInfo); procedure MuaNha(Index : Longint); procedure ThueNha(Index : Longint; NgayDau, NgayCuoi : TDateTime); procedure DeleteNha(Index : Longint); procedure SendMessageToServer(Socket : TCustomWinSocket; mess : pchar; index : longint; home : THomeInfo); procedure SendMessageToClient(Socket : TCustomWinSocket; mess : pchar; index : longint; home : THomeInfo); procedure ChangeIndex(var msg : TMessage); message WM_ChangeIndex; public { Public declarations } procedure DrawShadowBox(fCanvas : TCanvas; R: TRect); procedure DrawShadow(fCanvas : TCanvas; R: TRect); procedure PaintBack(fCanvas : TCanvas; B : TBitmap); end; type TMyHintWindow = class(THintWindow) constructor Create(AOwner : TComponent); override; end; type xxType = packed record mess : array[0..19] of char; index : longint; Home : THomeInfo; end; const sz = sizeof(xxType); idGetData : pchar = '$GetData$'; idSetData : pchar = '$SetData$'; idGetItem : pchar = '$GetItem$'; idSetItem : pchar = '$SetItem$'; idGetVLBNumItems : pchar = '$GetVLBNumItems$'; idSetVLBNumItems : pchar = '$SetVLBNumItems$'; idSetPageControl : pchar = '$idSetPageControl$'; idInsertData : pchar = '$idInsertData$'; idModifyData : pchar = '$idModifyData$'; idDelete : pchar = '$idDelete$'; idMuaNha : pchar = '$idMuaNha$'; idThueNha : pchar = '$idThueNha$'; idTimNha : pchar = '$idTimNha$'; idFind : pchar = '$idfind$'; idFindResult : pchar = '$idFindResult$'; idPaintVLB : pchar = '$idPaintVLB$'; idSetItemFilter : pchar = '$idSetItemFilter$'; idGetItemFilter : pchar = '$idGetItemFilter$'; idSetIndex : pchar = '$idSetIndex$'; Received : Boolean = false; DataReceived : Boolean = false; ItemReceived : Boolean = false; ClientConnected : Boolean = false; ListenChecked : Boolean = False; IsServer : Boolean = false; VLBClicking : boolean = false; var ChatForm: TChatForm; Server: String; CritSect : TRTLCriticalSection; implementation uses conectdlg, repthuef; {$R *.DFM} function GetPath : string; var i : byte; begin result := ParamStr(0); i := length(result); while (i > 0) and (result[i] '\') do dec(i); result := copy(result, 1, i); end; procedure WaitForReceived; begin repeat Application.ProcessMessages; until Received; end; constructor TMyHintWindow.Create(Aowner:TComponent); begin inherited Create(Aowner); Canvas.Font.Name := 'Vietnam'; Canvas.Font.Size := 8; end; procedure TChatForm.ModifyData(index : longint; Home : THomeInfo); begin if HomeData.HomeModify(home, index) then ServerUpdateVLB else VMessageDlg('Lši thuˆ nh… "' + Home.TenNha + '"', mtInformation, [mbOk], 0); end; procedure TChatForm.InsertData; var Index : longint; begin HomeData.HomeInsert(Home, index); ServerUpdateVLB; end; procedure TChatForm.MuaNha(Index : Longint); begin if HomeData.HomeSell(index) then ServerUpdateVLB else VMessageDlg('Lši mua nh… "' + fHome.TenNha + '"', mtInformation, [mbOk], 0); end; procedure TChatForm.ThueNha(Index : Longint; NgayDau, NgayCuoi : TDateTime); begin if HomeData.HomeRent(index, NgayDau, NgayCuoi) then ServerUpdateVLB else VMessageDlg('Lši thuˆ nh… "' + fHome.TenNha + '"', mtInformation, [mbOk], 0); end; procedure TChatForm.DeleteNha(Index : Longint); begin if HomeData.HomeDelete(index) then ServerUpdateVLB else VMessageDlg('Lši delete nh… "' + fHome.TenNha + '"', mtInformation, [mbOk], 0); end; procedure TChatForm.PaintBack(fCanvas : TCanvas; B : TBitmap); var X, Y, W, H: LongInt; begin with B do begin W := Width; H := Height; end; Y := 0; while Y <= Height do begin X := 0; while X <= Width do begin fCanvas.Draw(X, Y, B); Inc(X, W); end; Inc(Y, H); end; end; procedure TChatForm.DrawShadowBox(fCanvas : TCanvas; R: TRect); begin with fCanvas do begin Brush.Style := bsSolid; FillRect(R); Pen.Width := 3; Pen.Style := psSolid; Pen.Color := 155548 or $02000000; MoveTo(R.Left + 6, R.Bottom + 1); LineTo(R.Right + 1, R.Bottom + 1); LineTo(R.Right + 1, R.Top + 6); end; end; procedure TChatForm.DrawShadow(fCanvas : TCanvas; R: TRect); begin with fCanvas do begin Brush.Style := bsSolid; Brush.Color := 15007741 or $02000000; Pen.Width := 3; Pen.Style := psSolid; Pen.Color := 11548; MoveTo(R.Left + 6, R.Bottom + 1); LineTo(R.Right + 1, R.Bottom + 1); LineTo(R.Right + 1, R.Top + 6); end; end; procedure TChatForm.FormPaint(Sender: TObject); begin PaintBack(Canvas, Image1.Picture.Bitmap); DrawShadow(Canvas, Bevel2.BoundsRect); DrawShadow(Canvas, Bevel3.BoundsRect); end; function TChatForm.TestTLTBtn(p : TPoint) : boolean; var i : integer; begin result := false; for i := 0 to ControlCount - 1 do begin if (Controls[i] is TLTBtn) and (PtInRect((Controls[i] as TLTBtn).BoundsRect, p)) then Exit; if (Controls[i] is TQFButton) and (PtInRect((Controls[i] as TQFButton).BoundsRect, p)) then Exit; end; result := true; end; procedure TChatForm.wmNcHitTest(var Msg : TwmNcHitTest); begin inherited; if TestTLTBtn(ScreenToClient(Point(Msg.Pos.X, Msg.Pos.Y))) then Msg.Result := HTCAPTION; end; procedure TChatForm.DrawForm; begin VLB.Height := 170; Bevel4.Top := 287; case Level of 0: begin CusLTBtnFind.Visible := true; CusLTBtnThue.Visible := true; CusLTBtnBan.Visible := true; CusLTBtnFind.Top := 288; CusLTBtnThue.Top := 288; CusLTBtnBan.Top := 288; end; 1: begin DicLTBtnAdd.Visible := true; DicLTBtnDel.Visible := true; DicLTBtnModify.Visible := true; DicLTBtnAdd.Top := 288; DicLTBtnDel.Top := 288; DicLTBtnModify.Top := 288; end; 2: begin AdminLTBtnAdd.Visible := true; AdminLTBtnDel.Visible := true; AdminLTBtnModify.Visible := true; AdminLTBtnRepThue.Visible := true; AdminLTBtnRepBan.Visible := true; AdminLTBtnAdd.Top := 288; AdminLTBtnDel.Top := 288; AdminLTBtnModify.Top := 288; AdminLTBtnRepThue.Top := 288; AdminLTBtnRepBan.Top := 288; end; end; end; procedure TChatForm.FormCreate(Sender: TObject); var Level : integer; begin Level := ConnectDlg.Level.ItemIndex; DrawForm(Level); HomeData := nil; HintWindowClass := TMyHintWindow; Application.ShowHint := True; FileListenItemClick(nil); Server := ConnectDlg.HostName.Text; case Level of 2 : ConnectAdmin; 1 : ConnectModifier; 0 : ConnectCustomer; end; end; function TChatForm.FindClient( ClientSocket : TCustomWinSocket) : integer; begin for result := 0 to ServerSocket.Socket.ActiveConnections do if ServerSocket.Socket.Connections[result] = ClientSocket then Break; end; procedure TChatForm.ConnectAdmin; begin isServer := true; homeData := THomeData.Create; VLB.NumItems := HomeData.HomeGetCount; end; procedure TChatForm.ConnectModifier; begin with ClientSocket do begin Host := Server; Active := True; repeat application.ProcessMessages; until ClientConnected; Received := false; SendMessageToServer(Socket, idGetVLBNumItems, 0, fHome); WaitForReceived; end; end; procedure TChatForm.ConnectCustomer; begin with ClientSocket do begin Host := Server; Active := True; repeat application.ProcessMessages; until ClientConnected; Received := false; SendMessageToServer(Socket, idGetVLBNumItems, 0, fHome); WaitForReceived; end; end; procedure TChatForm.FileListenItemClick(Sender: TObject); begin ListenChecked := not ListenChecked; if ListenChecked then begin ClientSocket.Active := False; ServerSocket.Active := True; end else begin if ServerSocket.Active then ServerSocket.Active := False; end; end; procedure TChatForm.ShowData; var rtf : TMemoryStream; begin LabelName.Caption := fHome.TenNha; rtf := TMemoryStream.Create; RecToRTF(fHome, rtf); RichEdit1.Lines.LoadFromStream(rtf); rtf.Free; end; procedure TChatForm.SendMessageToServer(Socket : TCustomWinSocket; mess : pchar; index : longint; home : THomeInfo); var x : xxType; begin fillchar(x, sz, 0); move(mess^, x.mess, strlen(mess)); x.Home := Home; x.index := index; Socket.SendBuf(x, sz); end; procedure TChatForm.SendMessageToClient(Socket : TCustomWinSocket; mess : pchar; index : longint; home : THomeInfo); var x : xxType; begin fillchar(x, sz, 0); move(mess^, x.mess, strlen(mess)); x.Home := Home; x.index := index; Socket.SendBuf(x, sz); end; function TChatForm.SetHomeInfo(hasDefault : boolean) : boolean; begin result := false; with THomeInfoForm.Create(self) do begin if hasDefault then begin with fHome do begin EditTenNha.Text := TenNha; EditCapNha.Text := myIntToStr(CapNha); EditChieuDai.Text := myRealToStr(ChieuDai); EditChieuRong.Text := myRealToStr(ChieuRong); EditTangLau.Text := myIntToStr(TangLau); EditPhongKhach.Text := myIntToStr(PhongKhach); EditPhongNgu.Text := myIntToStr(PhongNgu); EditPhongTam.Text := myIntToStr(PhongTam); EditDiaChi.Text := DiaChi; EditDuong.Text := Duong; EditThanhPho.Text := ThanhPho; EditQuan.Text := Quan; chkNhaChoThue.checked := NhaChoThue; EditGiaMoiThang.Text := myRealToStr(GiaMoiThang); chkNhaBan.Checked := NhaBan; EditTriGia.Text := myRealToStr(TriGia); chkNhaNgoaiThanh.Checked := NhaNgoaiThanh; chkCoNuocMay.Checked := CoNuocMay; chkNhaCoHeThongNuocNong.Checked := NhaCoHeThongNuocNong; chkNhaCoHoBoi.Checked := NhaCoHoBoi; chkNhaCoNuocGieng.Checked := NhaCoNuocGieng; chkNhaTrongHem.Checked := NhaTrongHem; EditChieuRongHem.Text := myRealToStr(ChieuRongHem); EditVaoHemBaoSau.Text := myRealToStr(VaoHemBaoSau); DateTimePickerNamXayDung.Date := NamXayDung; comboTinhTrangNha.ItemIndex := TinhTrangNha; comboTrangTriNoiThat.ItemIndex := TrangTriNoiThat; chkNhaCoVuon.Checked := NhaCoVuon; EditChieuRongVuon.Text := myRealToStr(ChieuRongVuon); comboHuongNha.ItemIndex := HuongNha; comboChuquyen.ItemIndex := Chuquyen; end; end; if ShowModal = mrOK then begin result := true; fillchar(fHome, sizeof(fHome), 0); with fHome do begin TenNha := EditTenNha.Text; // 01 CapNha := myStrToInt(EditCapNha.Text); // 02 ChieuDai := myStrToReal(EditChieuDai.Text); ChieuRong := myStrToReal(EditChieuRong.Text); DienTich := ChieuDai * ChieuRong; TangLau := myStrToInt(EditTangLau.Text); PhongKhach := myStrToInt(EditPhongKhach.Text); PhongNgu := myStrToInt(EditPhongNgu.Text); PhongTam := myStrToInt(EditPhongTam.Text); Diachi := EditDiaChi.Text; Duong := EditDuong.Text; ThanhPho := EditTHanhPho.Text; Quan := EditQuan.Text; NhaChoThue := chkNhaChoThue.Checked; GiaMoiThang := myStrToReal(EditGiaMoiThang.Text); RaVND(GiaMoithang, ComboTienThueTinhTheo.ItemIndex); NhaBan := chkNhaBan.Checked; TriGia := myStrToReal(EditTriGia.Text); RAVND(TriGia, ComboTienBanTinhTheo.ItemIndex); NhaNgoaiThanh := chkNhaNgoaiThanh.Checked; CoNuocMay := chkCoNuocMay.Checked; NhaCoHeThongNuocNong := chkNhaCoHeThongNuocNong.Checked; NhaCoHoBoi := chkNhaCoHoBoi.Checked; NhaCoNuocGieng := chkNhaCoNuocGieng.Checked; NhaTrongHem := chkNhaTrongHem.Checked; ChieuRongHem := myStrToReal(EditChieuRongHem.Text); VaoHemBaoSau := myStrToReal(EditVaoHemBaoSau.Text); NamXayDung := DateTimePickerNamXayDung.Date; TinhTrangNha := ComboTinhTrangNha.ItemIndex; TrangTriNoiThat := ComboTrangTriNoiThat.ItemIndex; NhaCoVuon := chkNhaCoVuon.Checked; ChieurongVuon := myStrToReal(EditChieuRongVuon.Text); HuongNha := ComboHuongNha.ItemIndex; Chuquyen := ComboChuquyen.ItemIndex; Sell_Roi := False; Rent_Roi := False; NgayDauThue := -1; NgayCuoiThue := -1; end; end; Release; end; end; procedure TChatForm.ServerUpdateVLB; var i : integer; begin VLB.OnGetItem := nil; VLB.OnClick := nil; VLB.NumItems := HomeData.HomeGetCount; for i := 0 to ServerSocket.Socket.ActiveConnections - 1 do SendMessageToClient(ServerSocket.Socket.Connections[i], idSetVLBNumItems, VLB.NumItems, fHome); VLB.OnGetItem := VLBGetItem; VLB.OnClick := VLBClick; end; procedure TChatForm.VLBGetItem(Sender: TObject; Index: Integer; var ItemString: String); begin if (index < 0) then exit; if isServer then with THomeGetThread.Create(Index, True, -1) do WaitFor else begin ItemReceived := False; SendMessageToServer(ClientSocket.Socket, idGetItem, Index, fHome); repeat Application.ProcessMessages; until ItemReceived; end; ItemString := ffHome.TenNha; end; procedure TChatForm.VLBClick(Sender: TObject); var index : longint; begin index := VLB.ItemIndex; if (index < 0) or (self = nil) then exit; if isServer then with THomeGetDataThread.Create(Index, True, -1) do WaitFor else begin Received := False; SendMessageToServer(ClientSocket.Socket, idGetData, Index, fHome); WaitForReceived; end; ShowData; end; procedure TChatForm.ClientSocketConnect(Sender: TObject; Socket: TCustomWinSocket); begin ClientConnected := true; end; procedure TChatForm.ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket); var x : xxType; begin fillchar(x, sz, 0); Socket.ReceiveBuf(x, sz); if StrComp(x.mess, idSetVLBNumItems) = 0 then begin VLB.NumItems := x.Index; VLB.Repaint; Received := true; SendmessageToServer(Socket, idPaintVLB, -1, fhome); Exit; end; if StrComp(x.mess, idSetData)= 0 then begin fHome := x.Home; Received := true; DataReceived := true; Exit; end; if StrComp(x.mess, idSetITem)= 0 then begin ffHome := x.Home; ItemReceived := true; Exit; end; if StrComp(x.mess, idSetItemFilter)= 0 then begin FindForm.ffHome := x.Home; ItemReceived := true; Exit; end; if StrComp(x.mess, idFindResult)= 0 then begin if FindForm.Filter nil then Freemem(FindForm.Filter); Getmem(FindForm.Filter, x.index * sizeof(longint)); Move(x.Home, FindForm.Filter^, x.index * sizeof(longint)); FindForm.FindResult := true; FindForm.VLBFilter.NumItems := x.index; FindForm.VLBFilter.ItemIndex:= 0; FindForm.VLBFilter.Repaint; Exit; end; if StrComp(x.mess, idSetIndex)= 0 then begin VLB.ItemIndex := x.Index; end; end; procedure TChatForm.ServerSocketClientRead(Sender: TObject; Socket: TCustomWinSocket); var Client : integer; Count : longint; x : xxType; Find : TFind; begin fillchar(x, sz, 0); Socket.ReceiveBuf(x, sz); Client := FindClient(Socket); if StrComp(x.mess, idGetItem) = 0 then begin with THomeGetThread.Create(x.Index, False, Client) do WaitFor; Exit; end; if StrComp(x.mess, idGetItemFilter) = 0 then begin with THomeGetFilterThread.Create(x.Index, False, Client) do WaitFor; Exit; end; if StrComp(x.mess, idGetData) = 0 then begin with THomeGetDataThread.Create(x.Index, False, Client) do WaitFor; Exit; end; if StrComp(x.mess, idGetVLBNumItems) = 0 then begin Count := VLB.NumItems; SendMessageToClient(ServerSocket.Socket.Connections[Client], idSetVLBNumItems, Count, fHome); exit; end; if StrComp(x.mess, idInsertData)= 0 then begin with TInsertDataThread.Create(x.home) do WaitFor; Exit; end; if StrComp(x.mess, idModifyData)= 0 then begin with TModifyDataThread.Create(x.index, x.home) do WaitFor; Exit; end; if StrComp(x.Mess, idMuaNha) = 0 then begin with TMuaNhaThread.Create(x.index) do WaitFor; Exit; end; if StrComp(x.Mess, idThueNha) = 0 then begin with TThueNhaThread.Create(x.index, x.Home.NgayDauThue, x.Home.NgayCuoiThue ) do WaitFor; Exit; end; if StrComp(x.Mess, idDelete) = 0 then begin with TDeleteThread.Create(x.index) do WaitFor; Exit; end; if StrComp(x.Mess, idPaintVLB) = 0 then begin VLB.Repaint; Exit; end; if StrComp(x.Mess, idFind) = 0 then begin Move(x.Home, Find, Sizeof(Find)); with TFindThread.Create(Find, Client) do WaitFor; Exit; end; end; procedure TChatForm.CusLTBtnFindClick(Sender: TObject); begin FindForm := TFindForm.Create(self); with FindForm do begin ShowModal; Release; end; end; procedure TChatForm.CusLTBtnThueClick(Sender: TObject); var index : longint; begin if not fhome.NhaChoThue then begin ShowInfo('Nh… "' + fHome.TenNha + '" kh“ng cho thuˆ!', false); Exit; end; with TInputThue.Create(self) do if Showmodal = mrOK then begin index := VLB.ItemIndex; if index < 0 then Exit; fHome.NgayDauThue := DateTimePickerDau.Date; fHome.NgayCuoiThue := DateTimePickerCuoi.Date; SendMessageToServer(ClientSocket.Socket, idThueNha, index, fHome); end; end; procedure TChatForm.CusLTBtnBanClick(Sender: TObject); var index : longint; begin index := VLB.ItemIndex; if index < 0 then Exit; if not fHome.NhaBan then begin ShowInfo('Nh… "' + fHome.TenNha + '" kh“ng bÿn!', false); Exit; end; if ShowInfo( 'Bân c¢ mu–n ­èng kỵ mua nh… tˆn "' + fHome.TenNha + '" kh“ng?', true) = mrOK then SendMessageToServer(ClientSocket.Socket, idMuaNha, index, fHome); end; procedure TChatForm.DicLTBtnModifyClick(Sender: TObject); var Index : Longint; begin Index := VLB.ItemIndex; if Index < 0 then Exit; if SetHomeInfo(true) then begin SendMessageToServer(ClientSocket.Socket, idModifyData, Index, fHome); end; end; procedure TChatForm.DicLTBtnDelClick(Sender: TObject); var index : longint; begin if ShowInfo('Bân c¢ mu–n x¢a nh… tˆn "' + fHome.TenNha + '" kh“ng?', true) = mrOK then begin index := VLB.ItemIndex; if index < 0 then Exit; SendMessageToServer(ClientSocket.Socket, idDelete, index, fHome); end; end; procedure TChatForm.DicLTBtnAddClick(Sender: TObject); begin if SetHomeInfo(false) then begin SendMessageToServer(ClientSocket.Socket, idInsertData, 0, fHome); Received := False; WaitForReceived; end; end; procedure TChatForm.AdminLTBtnAddClick(Sender: TObject); begin if SetHomeInfo(false) then with TInsertDataThread.Create(fhome) do WaitFor; end; procedure TChatForm.AdminLTBtnDelClick(Sender: TObject); var index : longint; begin if ShowInfo('Bân c¢ chéc chén mu–n x¢a nh… tˆn "' + fHome.TenNha + '" kh“ng?', true) = mrOK then begin index := VLB.ItemIndex; if index < 0 then Exit; with TDeleteThread.Create(index) do WaitFor; end; end; procedure TChatForm.AdminLTBtnModifyClick(Sender: TObject); var Index : Longint; begin Index := VLB.ItemIndex; if Index < 0 then Exit; if SetHomeInfo(true) then with TModifyDataThread.Create(index, fhome) do WaitFor; end; procedure TChatForm.TextSearchChange(Sender: TObject); begin with THomeSearchTextThread.Create(TextSearch.Text, true, -1) do WaitFor; VLB.ItemIndex := findex; end; procedure TChatForm.TextSearchKeyPress(Sender: TObject; var Key: Char); begin if Key = #13 then ShowData; end; procedure TChatForm.AdminLTBtnRepThueClick(Sender: TObject); begin RepThueForm := TRepThueForm.Create(self); with RepThueForm do begin isThue := true; ShowModal; Release; end; end; procedure TChatForm.AdminLTBtnRep

Các file đính kèm theo tài liệu này:

  • docDA0669.doc
Tài liệu liên quan