线程怎么会莫名退出(100分)

  • 主题发起人 主题发起人 SonicX
  • 开始时间 开始时间
S

SonicX

Unregistered / Unconfirmed
GUEST, unregistred user!
我写这个线程经常会自动退出,不知什么毛病,还请高指点
这个线程的目的是在我程序运行的时候捕捉OUTLOOK警告窗口,如果找到就做点处理
TSearchOL = class(TThread)
private
OldOLhwnd:HWND;
protected
procedure Execute;override;
Procedure ThreadTerminate(Sender : TObject);
end;
..................
procedure TSearchOL.Execute;
var
OLhwnd : HWND;
begin
Priority := tpHigher;
FreeOnTerminate := True;
OnTerminate := ThreadTerminate;
While not Terminateddo
begin
OLhwnd:=FindWindow(Pchar('#32770'),Pchar('Microsoft Outlook'));
//Outlook2002
if OLhwnd=0 then
OLhwnd:=FindWindow(Pchar('#32770'),Pchar('Microsoft Office Outlook'));
//Outlook2003
//去掉以上2句线程就正常了,难道是Findwindow的问题?
if OLhwnd=0 then
OldOLhwnd:=OLhwnd;
if (OLhwnd<>0) and (OLhwnd<>OldOLhwnd) then
begin
if GetDlgItem(OLhwnd,4772)=0 then
break;
//判断是否是警告窗口
OldOLhwnd:=OLhwnd;
//储存窗口句柄
ShowWindow(OLhwnd,SW_HIDE);
//先隐藏这个窗口
//文字
SetWindowPos(GetDlgItem(OLhwnd,4772),0,0,0,280,30,SWP_NOMOVE or SWP_NOZORDER);
SetDlgItemText(OLhwnd,4772,Pchar('即将访问Microsoft Outlook,出与安全考虑请再次确认!'));
ShowWindow(GetDlgItem(OLhwnd,4775),SW_HIDE);
//否 隐藏
ShowWindow(GetDlgItem(OLhwnd,4776),SW_HIDE);
//帮助 隐藏
if SendDlgItemMessage(OLhwnd,4771,BM_GETCHECK,0,0)=BST_UNCHECKED then
begin
//允许访问 打钩
SendDlgItemMessage(OLhwnd,4771,WM_LBUTTONDOWN,0,0);
SendDlgItemMessage(OLhwnd,4771,WM_LBUTTONUP,0,0);
end;
SendDlgItemMessage(OLhwnd,4773,CB_SETCURSEL,SendDlgItemMessage(OLhwnd,4773,CB_GETCOUNT,0,0)-1,0);
//选择时间最长
SetWindowPos(GetDlgItem(OLhwnd,4774),0,142,55,0,0,SWP_NOSIZE or SWP_NOZORDER);
//移动 &quot;是&quot;
按钮
SetDlgItemText(OLhwnd,4774,Pchar('确定'));
//&quot;是' 改为 &quot;确定&quot;
//显示窗口设置
SetWindowLong(OLhwnd,GWL_STYLE,GetWindowLong(OLhwnd,GWL_STYLE ) and not WS_SYSMENU);
sleep(1);
SetWindowPos(OLhwnd,HWND_TOPMOST,0,0,360,120,SWP_NOMOVE or SWP_SHOWWINDOW);
end;
Application.ProcessMessages;
sleep(100);
end;
end;

Procedure TSearchOL.ThreadTerminate(Sender : TObject);
begin
if not Terminated then
begin
//我没有Terminated但经常会自动free,妖怪!!!!!
messagebox(Application.MainForm.Handle,pchar('error free'),nil,0);
end;
end;
 
关注.........,帮顶
 
线程对FindWindow的影响。用synchronize括起来你发现问题的那两句,行吗?
我怀疑那是因为线程对窗体的操作本来就不对。
 
SetWindowPos的时候,尝试设置一下这个参数:SWP_ASYNCWINDOWPOS
 
解决了,是
if GetDlgItem(OLhwnd,4772)=0 then
break;
break就退出循环了
 
多人接受答案了。
 
后退
顶部