B
bfox
Unregistered / Unconfirmed
GUEST, unregistred user!
最近在做一个多串口通信的软件,多线程调用一个串口通信的COM组件.基本思路是:因为初始化串口需要较长时间,所以,首先动态的创建多个挂起的线程并保存在线程数组里.然后当有数据需要发送时,从线程数组中选择一个,设置需要传输的信息内容,然后RESUME线程,并将线程从数组里删除.当线程完成工作后,触发主线程的ThreadDone事件,然后主线程将其添加到数组中去.
可是在线程被RESUME后,总是不能触发ThreadDone事件,哪位高手做过类似的项目啊?请指教!
public
{ Public declarations }
comportcount:Integer;
ComPortDeviceArray:array of TNewComPortDevice;
procedure ThreadDone(Sender: TObject);
procedure Tfrm_main.btn_initClick(Sender: TObject);
var
i:integer;
ComPortDevice:TNewComPortDevice;
begin
btn_init.Enabled:=False;
comportcount:=StrToInt(edt_comportcount.Text);
setlength(ComPortDeviceArray,comportcount);
UseAblePortCount:=comportcount;
for i:= 0 to comportcount-1do
begin
ComPortDevice:=TNewComPortDevice.create(i+1);
ComPortDevice.OnTerminate:=ThreadDone;
ComPortDeviceArray:=ComPortDevice;
end;
btn_send.
Enabled:=True;
end;
procedure Tfrm_main.ThreadDone(Sender: TObject);
begin
....
btn_send.
Enabled:=True;
end;
procedure Tfrm_main.btn_sendClick(Sender: TObject);
begin
btn_send.
Enabled:=False;
...
ComPortDevice.msg:='aabbccdd';
ComPortDevice.Resume;
end;
//这是线程的创建函数
constructor TNewComDevice.create(comport: Integer);
begin
Fcomport:=comport;
FreeOnTerminate := False;
inherited Create(true);
ComDevice := createoleobject('ComDevice.ComDevice1');
ComDevice.Connect(1, 19200);
end;
//这是线程的执行函数.用sleep (500)和ComDevice .SendMsg( Msg);都触发不了ThreadDone消息
procedure TNewsms.Execute;
var
i:Integer;
begin
sleep (500);
//ComDevice .SendMsg( Msg);
{ Place thread code here }
end;
可是在线程被RESUME后,总是不能触发ThreadDone事件,哪位高手做过类似的项目啊?请指教!
public
{ Public declarations }
comportcount:Integer;
ComPortDeviceArray:array of TNewComPortDevice;
procedure ThreadDone(Sender: TObject);
procedure Tfrm_main.btn_initClick(Sender: TObject);
var
i:integer;
ComPortDevice:TNewComPortDevice;
begin
btn_init.Enabled:=False;
comportcount:=StrToInt(edt_comportcount.Text);
setlength(ComPortDeviceArray,comportcount);
UseAblePortCount:=comportcount;
for i:= 0 to comportcount-1do
begin
ComPortDevice:=TNewComPortDevice.create(i+1);
ComPortDevice.OnTerminate:=ThreadDone;
ComPortDeviceArray:=ComPortDevice;
end;
btn_send.
Enabled:=True;
end;
procedure Tfrm_main.ThreadDone(Sender: TObject);
begin
....
btn_send.
Enabled:=True;
end;
procedure Tfrm_main.btn_sendClick(Sender: TObject);
begin
btn_send.
Enabled:=False;
...
ComPortDevice.msg:='aabbccdd';
ComPortDevice.Resume;
end;
//这是线程的创建函数
constructor TNewComDevice.create(comport: Integer);
begin
Fcomport:=comport;
FreeOnTerminate := False;
inherited Create(true);
ComDevice := createoleobject('ComDevice.ComDevice1');
ComDevice.Connect(1, 19200);
end;
//这是线程的执行函数.用sleep (500)和ComDevice .SendMsg( Msg);都触发不了ThreadDone消息
procedure TNewsms.Execute;
var
i:Integer;
begin
sleep (500);
//ComDevice .SendMsg( Msg);
{ Place thread code here }
end;