线程问题(100分)

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

qinzi5237

Unregistered / Unconfirmed
GUEST, unregistred user!
我有两个线程,这两个线程序分别使用同一设备,,一个是启用加密,另一个是启用非加密的,,他们分别是用来读取用户从金属小键盘中输入的手机号码或密码..虽然两个线程我没有同时启用,我只在特定需要下启用特定的线程序,如要输入手机号码那么我就启用非加密来获取用户的输入.当要输入密码时我就启用加密线程来获取用户输入密码的密文,这两个线程都是在输入完后马上终止,但我发现在启用另外一个线程时设备串口没有被及时关闭,,请看看我的线程是否有问题,以下是我的代码:
//用来获取用户输入密码
unit BankEncrptThread;
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;
TZt598PinLoadMasterKey = function(pi_MKeyNo: integer;
MKeyAsc: pchar;
mode: char;
cpAuth: pchar): integer;
stdcall;
TZt598PinLoadWorkKey = function(pi_MKeyNo: integer;
pi_WKeyNo: integer;
pi_WkeyAsc: string;
pi_Mode: char;
cpAuth: pchar): 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;
TZt598PinAdd = function(pi_str: pchar;
nlen: integer;
po_str: pchar): integer;
stdcall;
TZt598PinUnAdd = function(pi_str: pchar;
len: integer;
po_str: pchar): integer;
stdcall;
TZt598SetDesPra = function(p_in: integer;
f_in: integer): integer;
stdcall;
TZt598PinLoadCardNo = function(CardNo: Pchar): integer;
stdcall;
TZt598PinAbord = function(): integer;
stdcall;
BankEncrptKeyThread = class(TThread)
private
{ Private declarations }
DllHins: Cardinal;
//动态库句柄
checkchar: array[0..20] of char;
Pinchar: array[0..20] of char;
function Zt598Open(pi_hins: Cardinal;
pi_nPort: integer;
pi_nComRate: integer): boolean;
function Zt598begin
Input(pi_hins: Cardinal;
pi_nlength: integer): boolean;
protected
procedure Execute;
override;
public
IsLoad: Boolean;
//是否载入动态库
IsOpen: Boolean;
//是否打开端口
IsValid: Boolean;
//是否激活键盘
ComNum: integer;
//com端口
ShowMsg: string;
isStop: boolean;
destructor Destroy;
override;
end;
implementation
uses PublicFuncVcl;
function BankEncrptKeyThread.Zt598begin
Input(pi_hins: Cardinal;
pi_nlength: integer): boolean;
var
tempString: array[1..20] of char;
ptempString: pchar;
ret, i: integer;
begin
result := true;
ptempString := @tempString[1];
ret := TZt598ActivWorkPin(GetProcAddress(pi_hins, 'ActivWorkPin'))(0, 0);
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '激活主密钥:' + inttostr(ret));
exit;
end;
ret := TZt598OpenKeyVoic(GetProcAddress(pi_hins, 'OpenKeyVoic'))(2);
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '打开密码键盘:' + inttostr(ret));
exit;
end;

ret := TZt598PinStartAdd(GetProcAddress(pi_hins, 'PinStartAdd'))(pi_nlength, 1, 1, 0, 90, ptempString);
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '启动加密键盘:' + inttostr(ret));
exit;
end;
end;

function BankEncrptKeyThread.Zt598Open(pi_hins: Cardinal;
pi_nPort: integer;
pi_nComRate: integer): boolean;
var
ret, i: integer;
cpAuth: array[1..17] of char;
chCardNo12: array[1..12] of char;
pCardno: pchar;
sttmp: string;
begin
result := true;
for i := 1 to 17do
cpAuth := #0;
if pubfuncvcl.keyboardIS then
begin
ret := TZt598CloseCom(GetProcAddress(pi_hins, 'gCloseCom'))();
if ret <> 0 then
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '关闭串口:' + inttostr(ret));
end;
ret := TZt598OpenCom(GetProcAddress(pi_hins, 'gOpenCom'))(pi_nPort, pi_nComRate, '02');
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '打开串口:' + inttostr(ret));
exit;
end;
ret := TZt598SetTimeOut(GetProcAddress(pi_hins, 'gSetTimeOut'))(60);
//设置10秒超时
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '设置超时:' + inttostr(ret));
exit;
end;
ret := TZt598SetDesPra(GetProcAddress(pi_hins, 'SetDesPra'))($01, $60);
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '设置参数:' + inttostr(ret));
exit;
end;
ret := TZt598PinLoadMasterKey(GetProcAddress(pi_hins, 'PinLoadMasterKey'))(0, pchar(pubfuncvcl.pin_key), chr(1), @cpAuth[1]);
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '载入主密钥:' + inttostr(ret));
exit;
end;
pCardno := @chCardNo12[1];
if pubfuncvcl.tack2str <> '' then
begin
sttmp := copy(pubfuncvcl.tack2str, pos('=', pubfuncvcl.tack2str) - 13, 12);
for i := 1 to 12do
chCardNo12 := sttmp;
end
else
begin
sttmp := copy(pubfuncvcl.tack3str, pos('=', pubfuncvcl.tack3str) - 13, 12);
for i := 1 to 12do
chCardNo12 := sttmp;
end;
ret := TZt598PinLoadCardNo(GetProcAddress(pi_hins, 'PinLoadCardNo'))(pCardno);
if ret <> 0 then
begin
result := false;
PubFuncVcl.WriteLogFile('./日志管理/终端日志/' + formatdatetime('yyyymmdd', now()) + '_err.log', DateTimeToStr(Now) + '~' + '载入卡号:' + inttostr(ret));
exit;
end;
end;

procedure BankEncrptKeyThread.Execute;
var
RetCode, i: integer;
Pcheckchar: Pchar;
POutPinchar: pchar;
OpenFlagBol: boolean;
ActionflagBol: boolean;
chPinBlock: array[0..255] of char;
begin
RetCode := 0;
isStop := false;
for i := 0 to 255do
chPinBlock := #0;
while truedo
begin

if (terminated = true) or (isStop = true) then
begin
break;
end;

//判断动态库是否已经载入,如果没有载入则打开载入
if not IsLoad then
try
DllHins := LoadLibrary('ZT598.DLL');
IsLoad := true;
except
IsLoad := false;
end;
//判断是否打开了com端口,没有打开则打开端口
if (IsLoad) and (not IsOpen) then
begin
OpenFlagBol := Zt598Open(DllHins, ComNum, 9600);
//打开端口
if OpenFlagBol then
begin
IsOpen := true;
pubfuncvcl.keyboardIS := true;
end
else
IsOpen := false;
end;
//判断是否激活pinpad,没有打开则激活pinpad
if (IsLoad) and (IsOpen) and (not IsValid) then
begin
ActionflagBol := Zt598begin
Input(DllHins, 7);
//激活pinpad
if ActionflagBol 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($0D) then
//按了确认
begin
TZt598PinReadPin(GetProcAddress(DllHins, 'PinReadPin'))(@chPinBlock[0]);
pubfuncvcl.userpin := pubfuncvcl.strtohex(strpas(@chPinBlock[0]));
SendMessage(Screen.ActiveControl.Handle, WM_CHAR, Ord(checkchar[0]), 0);
//将获取的键盘信息送到窗口的相关句柄中
break;
end;
SendMessage(Screen.ActiveControl.Handle, WM_CHAR, Ord(checkchar[0]), 0);
//将获取的键盘信息送到窗口的相关句柄中
end;
end;
end;
end;
destructor BankEncrptKeyThread.Destroy;
var
ret: integer;
begin
try
ret := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(0);
ret := TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'));
finally
freelibrary(DllHins);
IsLoad := false;
pubfuncvcl.keyboardIS := false;
end;
inherited;
end;
end.

//获取用户输入号码的线程
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端口
destructor Destroy;
override;
end;
implementation
uses PublicFuncVcl;
destructor MyEncrptKeyThread.Destroy;
var
ret: integer;
begin
try
ret := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(0);
ret := TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'));
finally
freelibrary(DllHins);
IsLoad := false;
pubfuncvcl.keyboardIS := false;
end;
inherited;
end;

procedure MyEncrptKeyThread.Execute;
var
RetCode: integer;
Pcheckchar: Pchar;
begin
RetCode := 0;
isStop := false;
while truedo
begin

if (terminated = true) or (isStop = true) then
begin

break;
end;
//判断动态库是否已经载入,如果没有载入则打开载入
if not IsLoad then
try
DllHins := LoadLibrary('ZT598.DLL');
IsLoad := true;
except
IsLoad := false;
end;
//判断是否打开了com端口,没有打开则打开端口
if (IsLoad) and (not IsOpen) then
begin
application.ProcessMessages;
if pubfuncvcl.keyboardIS then

TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'))();
RetCode := TZt598OpenCom(GetProcAddress(DllHins, 'gOpenCom'))(ComNum, 9600, '02');
//打开端口
if RetCode = 0 then
begin
IsOpen := true;
pubfuncvcl.keyboardIS := true;
end
else
IsOpen := false;
end;
//判断是否激活pinpad,没有打开则激活pinpad
if (IsLoad) and (IsOpen) and (not IsValid) then
//if not IsValid then
begin
TZt598SetTimeOut(GetProcAddress(DllHins, 'gSetTimeOut'))(60);
//设置超时
RetCode := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(3);
//激活pinpad
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
SendMessage(Screen.ActiveControl.Handle, WM_CHAR, Ord(checkchar[0]), 0);
//将获取的键盘信息送到窗口的相关句柄中
end;

end;

end;
end;

end.

//这是用户来终止线程语句
if EncrptKeyThread11 <> nil then
begin
EncrptKeyThread11.isStop := true;
application.ProcessMessages;
if TerminateThread(EncrptKeyThread11.Handle, 0) then
EncrptKeyThread11.Free
else
EncrptKeyThread11.Terminate;
end;
//启用线程语句
comnum := strtoint(pubfuncvcl.getParam('keyboard', 'com'));
EncrptKeyThread11 := BankEncrptKeyThread.Create(true);
EncrptKeyThread11.ComNum := comnum;
EncrptKeyThread11.IsLoad := false;
EncrptKeyThread11.IsOpen := false;
EncrptKeyThread11.IsValid := false;
EncrptKeyThread11.Resume;
 
既然不同时运行那还用线程干吗,还不如用函数实现呢?
 
完全不需要多线程,纯粹是自己给自己找麻烦
 
帮顶!
http://www.source520.com
站长开发推广同盟 站长朋友的终极驿站
同时拥有海量源码电子经典书籍下载
http://www.source520.com/search/search.asp
&quot;编程.站长&quot;论坛搜索引擎-----为中国站长注入动力!
 
后退
顶部