线程WaitFor问题 ( 积分: 100 )

  • 主题发起人 主题发起人 qinzi5237
  • 开始时间 开始时间
Q

qinzi5237

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我写的代码,不知道为什么偶尔会执行到WaitFor语句时就一直死等在那里了
//线程代码
unit EncrptKeyThread;
interface
uses
Classes,
Windows, Messages, SysUtils, Variants, StdCtrls, dialogs, Controls, Forms;
type
TZt598OpenCom = function(pi_nPort:integer;
pi_nComRate:integer;
pi_control:pchar):integer;
stdcall;
//打开键盘串口
TZt598CloseCom = function():integer;
stdcall;
//关闭键盘串口
TZt598OpenKeyVoic = function(Ctl:integer):integer;
stdcall;
//激活键盘
TZt598SetTimeOut = function(pi_Second:integer):integer;
stdcall;
//设置超时
TZt598PinLoadWorkKey = function(pi_MKeyNo:integer;
pi_WKeyNo:integer;
pi_WkeyAsc:string;
pi_Mode:char):integer;
stdcall;
//载入主密钥
TZt598ActivWorkPin = function(pi_MKeyNo:integer;
pi_WKeyNo:integer):integer;
stdcall;
TZt598PinStartAdd = function(pi_PinLen:integer;
pi_DispMode:integer;
pi_AddMode:integer;
pi_PromMode:integer;
pi_TimeOut:integer;
pchOutPin:pchar):Integer;
stdcall;
TZt598PinReportPressed = function(po_char:pchar):Integer;
stdcall;
TZt598PinReadPin = function(po_str:pchar):integer;
stdcall;
TZt598PinUnAdd = function(pi_str:pchar;
po_str:pchar):integer;
stdcall;
TZt598PinAbord = function():integer;
stdcall;
MyEncrptKeyThread = class(TThread)
private
{ Private declarations }
DllHins:Cardinal;
//动态库句柄
checkchar:array[0..20] of char;
protected
procedure Execute;
override;
public
isStop:boolean;
IsLoad:Boolean;
//是否载入动态库
IsOpen:Boolean;
//是否打开端口
IsValid:Boolean;
//是否激活键盘
ComNum:integer;
//com端口
ShowMsg:string;
//destructor Destroy;
override;
procedure closeKeyVoic(Sender:TObject);
end;
implementation
procedure MyEncrptKeyThread.closeKeyVoic;
var
ret:integer;
begin
try
ret := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(0);
ret := TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'));
finally
freelibrary(DllHins);
IsLoad := false;
end;
end;

procedure MyEncrptKeyThread.Execute;
var
RetCode:integer;
Pcheckchar:Pchar;
begin
RetCode := 0;
while truedo
begin
if (terminated = true) or (isStop = true) then
begin
break;
end;
//判断动态库是否已经载入,如果没有载入则打开载入
if not IsLoad then
begin

try
DllHins := LoadLibrary('598.DLL');
IsLoad := true;
except
IsLoad := false;
end;

end;
//判断是否打开了com端口,没有打开则打开端口
if (IsLoad) and (not IsOpen) then
begin
try
TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'))();
//强制先关闭串口
finally
;
end;
RetCode := TZt598OpenCom(GetProcAddress(DllHins, 'gOpenCom'))(ComNum, 9600,
'02');
//打开端口
if RetCode = 0 then
begin
IsOpen := true;
end
else
IsOpen := false;
end;
//判断是否激活pinpad,没有打开则激活pinpad
if (IsLoad) and (IsOpen) and (not IsValid) then
begin
TZt598SetTimeOut(GetProcAddress(DllHins, 'gSetTimeOut'))(60);
//设置金属小键盘超时
RetCode := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(3);
//激活金属小键盘
if RetCode = 0 then
begin
IsValid := true;
end
else
IsValid := false;
end;
if IsLoad and IsOpen and IsValid then
begin
Pcheckchar := @checkchar[0];
checkchar[0] := #0;
RetCode := TZt598PinReportPressed(GetProcAddress(DllHins,
'PinReportPressed'))(Pcheckchar);
if RetCode = 0 then
begin

if (checkchar[0] = chr($2E))or (checkchar[0] = chr($2A))or (checkchar[0] = chr($23)) then
checkchar[0] := #0;
PostMessage(Screen.ActiveControl.Handle, WM_CHAR, Ord(checkchar[0]), 0);
//将获取的键盘信息送到窗口的相关句柄中
end;
end;
end;
end;
end.

//启动线程代码段
EncrptKeyThread1 := MyEncrptKeyThread.Create(true);
EncrptKeyThread1.ComNum := comnum;
EncrptKeyThread1.isStop:=false;
EncrptKeyThread1.IsLoad := false;
EncrptKeyThread1.IsOpen := false;
EncrptKeyThread1.IsValid := false;
EncrptKeyThread1.OnTerminate := EncrptKeyThread1.closekeyvoic;
EncrptKeyThread1.Resume;
//中止线程代码段
if EncrptKeyThread11 <> nil then
begin
EncrptKeyThread11.isStop := true;
EncrptKeyThread11.Terminate;
if EncrptKeyThread11.Suspended then
EncrptKeyThread11.Resume;
EncrptKeyThread11.WaitFor;
freeandnil(EncrptKeyThread11);
end;
 
写这么多 谁愿意看啊
 
是不是写病毒啊,太长了!
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3308527
 
找了半天也没看到waitfor,老了,眼神不好使了,唉
 
if EncrptKeyThread11 <> nil then
begin
EncrptKeyThread11.isStop := true;
EncrptKeyThread11.Terminate;
if EncrptKeyThread11.Suspended then
EncrptKeyThread11.Resume;
EncrptKeyThread11.WaitFor;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
freeandnil(EncrptKeyThread11);
end;

我不明白为什么要用waitfor呢?这里用waitfor的目的是什么呢?
 
从你的代码结构看,没什么问题,虽然细节上有一点小问题。
Delphi 的线程类,个人以为是 Delphi 中封装得最好的类之一,但经常看到使用不当的用例。
比如这个 WaitFor 方法,常用于等待线程实例的结束。但这种等待是有环境要求的:
由于线程类中直接提供了一个叫Synchronize的方法,这个方法需要主线程不能被挂起。这
也就是说,WaitFor 方法尽量在一个不影响主线程运行的环境中使用。我看到很多代码,都
是直接在主线程中 WaitFor,这种代码说不定什么时候就会得到应有的报应的,因为主线程
被挂起了,如果正好有其他线程在使用消息同步,或者线程实例的代码写得和楼主你一样的
“贪婪”,我想,他们总有那么一次“偶然”的机会会大家一起罢工休假的。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部