線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1759
推到 Plurk!
推到 Facebook!

在IdTCPServer的OnExecute事件中访问VCL需要同步吗?

尚未結案
sdshw
一般會員


發表:8
回覆:11
積分:3
註冊:2008-03-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-02-09 16:19:34 IP:123.232.xxx.xxx 訂閱
各位大师:
在IdTCPServer的OnExecute事件中访问VCL需要同步吗?
在Indy9 的Chat Demo中没有看到同步访问VCL的代码,这样做安全吗?
请看代码:

[code delphi]
procedure TfrmMain.tcpServerExecute(AThread: TIdPeerThread);
var
Client : TSimpleClient;
Com, // System command
Msg : String;
begin
{ Get the text sent from the client }
Msg := AThread.Connection.ReadLn;
{ Get the clients package info }
Client := Pointer(AThread.Data);
{ Check to see if the clients name has been assigned yet }
if Client.Name = 'Logging In' then
begin
{ if not, assign the name and announce the client }
Client.Name := Msg;
UpdateClientList; //这里面访问了TListBox
BroadcastMessage('System', Msg ' has just logged in.');
AThread.Connection.WriteLn(memEntry.Lines.Text);
end
else
{ If name is set, then send the message }
if Msg[1] <> '@' then
begin
{ Not a system command }
BroadcastMessage(Client.Name, Msg);
end
else
begin
{ System command }
Com := UpperCase(Trim(Copy(Msg, 2, Pos(':', Msg) -2)));
Msg := UpperCase(Trim(Copy(Msg, Pos(':', Msg) 1, Length(Msg))));
if Com = 'CLIENTS' then
AThread.Connection.WriteLn( '@' 'clients:'
lbClients.Items.CommaText);
end;
end;

[/code]

[code delphi]
procedure TfrmMain.UpdateClientList;
var
Count : Integer;
begin
{ Loop through all the clients connected to the system and set their names }
for Count := 0 to lbClients.Items.Count -1 do
if Count < Clients.Count then
lbClients.Items.Strings[Count] := TSimpleClient(Clients.Items[Count]).Name; //这儿访问了TListBox
end;

[/code]
sos_admin
版主


發表:121
回覆:697
積分:768
註冊:2003-07-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-03-05 15:30:05 IP:121.229.xxx.xxx 訂閱
OnExecute 可以启用单独线程处理客户请求,而你的代码中没有这么做,也就是说在同一线程中处理请求的,所以就没有采用同步机制。
sdshw
一般會員


發表:8
回覆:11
積分:3
註冊:2008-03-24

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-03-10 12:20:47 IP:123.232.xxx.xxx 訂閱
sos_admin 老师能说的详细点吗
===================引 用 sos_admin 文 章===================
OnExecute 可以启用单独线程处理客户请求,而你的代码中没有这么做,也就是说在同一线程中处理请求的,所以就没有采用同步机制。
系統時間:2024-03-28 18:33:10
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!