关于使用indyudp控件的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 匪匪
  • 开始时间 开始时间

匪匪

Unregistered / Unconfirmed
GUEST, unregistred user!
我用indyudp控件,在客户端接收消息与发送消息。我都只用了一个inUdpclient控件来进行处理,因为接受消息是不会停的,所以我用了个无限循环。同时在这个无限循环里面每循环一次,当收到一个消息时,就开一个线程对该消息进行处理。现在的问题是有时候经常出错,而且出错的原因很怪。当我在服务端运行多个实例时,也是这个错误。代码大概如下种原理:请大家帮我看看:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
JThreadhand: THandle;
JThreadId: Dword;
JChildThreadHand: THandle;
JChildThreadID: DWord;
CriticalSection: TRTLCriticalSection;
a: integer;
List: TStringList;
implementation

{$R *.dfm}
procedure ProcessMessage;
var
i: integer;
begin
with form1 do
begin

for i:=0 to 1000 do
begin
EnterCriticalSection(CriticalSection);
Label1.Caption := IntToStr(i);
LeaveCriticalSection(CriticalSection);
end;

end;
end;

procedure RecMessage;
var
I: integer;
begin
with Form1 do
begin
for i:= 0 to 200 do
begin
// label1.Left := Label1.Left + 3;
// Sleep(10);
InitializeCriticalSection(CriticalSection);

JChildThreadHand:=CreateThread(nil,0,@ProcessMessage,nil,0,JChildThreadID);
end;
end;
end;




procedure TForm1.FormCreate(Sender: TObject);
begin
a:= 1000;
List:= TStringList.Create;
JThreadhand:=CreateThread(nil,0,@RecMessage,nil,0,JThreadId);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
List.Free;
TerminateThread(JThreadhand, 4);
TerminateThread(JChildThreadHand,4);
end;

end.
 
我用indyudp控件,在客户端接收消息与发送消息。我都只用了一个inUdpclient控件来进行处理,因为接受消息是不会停的,所以我用了个无限循环。同时在这个无限循环里面每循环一次,当收到一个消息时,就开一个线程对该消息进行处理。现在的问题是有时候经常出错,而且出错的原因很怪。当我在服务端运行多个实例时,也是这个错误。代码大概如下种原理:请大家帮我看看:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
JThreadhand: THandle;
JThreadId: Dword;
JChildThreadHand: THandle;
JChildThreadID: DWord;
CriticalSection: TRTLCriticalSection;
a: integer;
List: TStringList;
implementation

{$R *.dfm}
procedure ProcessMessage;
var
i: integer;
begin
with form1 do
begin

for i:=0 to 1000 do
begin
EnterCriticalSection(CriticalSection);
Label1.Caption := IntToStr(i);
LeaveCriticalSection(CriticalSection);
end;

end;
end;

procedure RecMessage;
var
I: integer;
begin
with Form1 do
begin
for i:= 0 to 200 do
begin
// label1.Left := Label1.Left + 3;
// Sleep(10);
InitializeCriticalSection(CriticalSection);

JChildThreadHand:=CreateThread(nil,0,@ProcessMessage,nil,0,JChildThreadID);
end;
end;
end;




procedure TForm1.FormCreate(Sender: TObject);
begin
a:= 1000;
List:= TStringList.Create;
JThreadhand:=CreateThread(nil,0,@RecMessage,nil,0,JThreadId);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
List.Free;
TerminateThread(JThreadhand, 4);
TerminateThread(JChildThreadHand,4);
end;

end.
 
[:(]不要只看不回贴啊。
 
后退
顶部