★挑战CSDN高手★DELPHI中的线程使用!

wnhoo 2003-04-21 04:50:23
1.线程的定义
2.线程的操作
3.线程派生的类型详细解释及源码

4.程序中线程的最佳数量
5.在程序中是否应该使用线程?

还有2个挑战性的题目,有兴趣看看:
http://expert.csdn.net/Expert/topic/1685/1685414.xml?temp=.8516962
http://expert.csdn.net/Expert/topic/1685/1685390.xml?temp=.1144678

我有一个运行很慢的线程源码,请各位看看。顺帮我解释一下(1~5)


const_code.pas
//***********
unit const_code;
interface
var
userinfostr:string;

const server_code='_001';
const conn_buffer_clear='_000';
const conn_login='_001';
const conn_login_user_get='_101';
const conn_login_user_reg='_201';
const conn_login_user_check='_301';
const conn_test='_002';
const conn_scr='_003';
const conn_file='_004';
const conn_text='_005';
const conn_cmd='_006';
implementation
end.
//#####################################
unit2.pas
//****************************
unit Unit2;

interface

uses
Classes, StdCtrls, SysUtils, ScktComp,const_code, Dialogs;

type
TMyServerThread = class(TServerClientThread)
private
FMemo: TMemo;
FListBox,FListBox2,FListBox3,FListBox4, FListBox5, FListBox6 : TListBox;
Buffer: string;
buffersize:integer;

protected
procedure ClientExecute; override;
procedure DoDisplayBuffer;
public
constructor Create(Memo: TMemo; listbox2,listbox3,listbox4,listbox5,listbox6,ListBox: TListBox; CreateSuspended: Boolean; ASocket: TServerClientWinSocket);
end;

implementation
{ TMyServerThread }

constructor TMyServerThread.Create(Memo: TMemo;listbox2,listbox3,listbox4,listbox5,listbox6,ListBox: TListBox; CreateSuspended: Boolean; ASocket: TServerClientWinSocket);
begin
inherited Create(CreateSuspended,ASocket);
FMemo := Memo;
FListBox := ListBox;
FListBox2 :=ListBox2;
FListBox3 :=ListBox3;
FListBox4 :=ListBox4;
FListBox5 :=ListBox5;
FListBox6 :=ListBox6;
end;

procedure TMyServerThread.DoDisplayBuffer();
var
con: Integer;
begin
//在MEMO中显示客户端发送的信息
FMemo.Lines.Add('线程ID<' + IntToStr(threadID) +
'>远程客户端<' + ClientSocket.RemoteHost + ':' +
IntToStr(ClientSocket.RemotePort) +'> : ' + Buffer+'<'+datetimetostr(now())+'>' );
//将收到的消息发给每个客户端
if buffersize> 3 then
for con:=0 to ClientSocket.ServerWinSocket.ActiveConnections-1 do
ClientSocket.ServerWinSocket.Connections[con].SendText(copy(buffer,5,buffersize-4)+'<'+datetimetostr(now())+'>');

end;
procedure TMyServerThread.ClientExecute;
var
Stream : TWinSocketStream;
n:integer;
begin
//程序正在运行 ,并且CLIENTSOCKET是活动的
while (not Terminated) and ClientSocket.Connected do
begin
try
Stream := TWinSocketStream.Create(ClientSocket, 60000);
try
//填充BUFFER
// FillChar(Buffer,sizeof(buffer),0);

if Stream.WaitForData(60000) then
if buffersize=0 then ClientSocket.Close
else
begin
buffersize:=length(Buffer);
//缓冲清除
if copy(Buffer,1,4)=conn_buffer_clear then
Stream.CleanupInstance ;
//检测合法
if copy(Buffer,1,4)=conn_login_user_check then
begin
fListBox2.Items.Add(ClientSocket.RemoteHost);
fListBox3.Items.Add(ClientSocket.RemoteAddress) ;
fListBox4.Items.Add(inttostr(ClientSocket.RemotePort));
fListBox5.Items.Add(inttostr(threadID));
fListBox6.Items.Add(copy(buffer,5,buffersize-4));
Buffer:=conn_login_user_check+'<消息服务系统>:用户【'+copy(Buffer,5,buffersize-4)+'】已经上线。';
buffersize:=length(Buffer);
end;
//获取当前用户信息
if copy(Buffer,1,4)=conn_login_user_get then
begin
userinfostr:='';
for n:=0 to fListBox6.Count-1 do
userinfostr:=userinfostr+'【'+fListBox6.Items.Strings[N]+'】';
Buffer:=conn_login_user_get+userinfostr;
buffersize:=length(Buffer);
end;
//成功登陆
if copy(Buffer,1,4)=conn_login then ;



//发信息
synchronize(DoDisplayBuffer);
end;
finally
Stream.Free;
end;
except
HandleException;
end;
end;
end;


end.

挑战高手,完善自我!!
1.线程的定义
2.线程的操作
3.线程派生的类型详细解释及源码

4.程序中线程的最佳数量
5.在程序中是否应该使用线程?

还有2个挑战性的题目,有兴趣看看:
http://expert.csdn.net/Expert/topic/1685/1685414.xml?temp=.8516962
http://expert.csdn.net/Expert/topic/1685/1685390.xml?temp=.1144678
...全文
28 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ksaiy 2003-04-21
  • 打赏
  • 举报
回复
代码狂多!
halfdream 2003-04-21
  • 打赏
  • 举报
回复
冷水降温,这些都只是基础。
stick77 2003-04-21
  • 打赏
  • 举报
回复
不是线程运行慢,而是你在线程中用了WaitForData(60000)函数。在socket阻塞式通信中,只有服务器和客户程序双方都准备好了才能进行读写。如果服务器有数据到来,waifordata就会马上返回了,所以和客户程序有关系。
yansea 2003-04-21
  • 打赏
  • 举报
回复
建议你看一看候捷翻译的一本关于线程的书,名字我记不清楚了,虽然是很久以前写的,但内容很不错。

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧