线程问题求助 我在主窗口中的一个按钮点击事件中创建了一个线程,线程调用了主窗体的其他按钮的点击事件,在退出程序的报错,Exception EOSError in…… 及“无效的窗口句柄” 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先释放线程,再退出程序,在线程开始设FreeOnTerminate=true试试 procedure test.Execute;begin Synchronize(Form1.method());end; 使用starsky2006的方法,我的函数根本没有被执行 你如果 不要 传复杂参数用message更好 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Memo2: TMemo; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private FmyThread: TThread; public procedure test; end; TtestThread = class(TThread) private FFrm: TForm1; procedure doMethod; protected procedure Execute; override; public constructor Create(Frm: TForm1); end;var Form1: TForm1;implementation{$R *.dfm}{ TtestThread }constructor TtestThread.Create(Frm: TForm1);begin FFrm := Frm; FreeOnTerminate := true; inherited Create(true); Priority := tpLowest;end;procedure TtestThread.doMethod;begin FFrm.test;end;procedure TtestThread.Execute;begin while not Terminated do begin Sleep(100); Synchronize(doMethod); end;end;procedure TForm1.test;begin Memo1.Lines.Add(FormatDateTime('hh:NN:ss.zzz', now))end;procedure TForm1.Button1Click(Sender: TObject);begin FmyThread := TtestThread.Create(self); Button1.Enabled := false; FmyThread.Resume;end;procedure TForm1.FormCreate(Sender: TObject);begin FmyThread := nil;end;procedure TForm1.FormDestroy(Sender: TObject);begin if FmyThread <> nil then begin FmyThread.Terminate; end;end;end. 参照withcsharp() 的方法,退出程序时没有报错了,但是当程序执行的时候,不能点击鼠标,不然界面就失去响应了。怎样才让鼠标可以动呢? 线程不能调用界面事件驱动.你调用Synchronize(doMethod);是必须等待doMethod执行完毕才释放控制权,要不就一致等待,所以就产生界面死了的现象.把Synchronize去掉,直接调用doMethod. 关于文件保存的问题。 如何正确使用TClientDataSet TDataSetProvider TIdHTTP控件下载有自动转向的网页时,会出错,是没设置好吗? 自绘高手帮忙看看这段代码哪里可能没释放系统资源。此段代码运行足够次数后就提示系统资源耗尽。 请问如何用Windows默认的程序打开文件夹? 读取IE历史记录 关于MDI子窗口的布局问题? Delphi编译生成的应用程序为何会运行一会自动关闭? 如何控制FORM启动时将所有的combobox itemindex全设为0 桌面程序怎么与网站交互 找DELPHI4下载(收藏用) 一个串口通信的问题!请高手帮帮忙!
begin
Synchronize(Form1.method());
end;
用message更好
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FmyThread: TThread;
public
procedure test;
end;
TtestThread = class(TThread)
private
FFrm: TForm1;
procedure doMethod;
protected
procedure Execute; override;
public
constructor Create(Frm: TForm1);
end;
var
Form1: TForm1;implementation{$R *.dfm}
{ TtestThread }constructor TtestThread.Create(Frm: TForm1);
begin
FFrm := Frm;
FreeOnTerminate := true;
inherited Create(true);
Priority := tpLowest;
end;procedure TtestThread.doMethod;begin
FFrm.test;
end;procedure TtestThread.Execute;
begin
while not Terminated do
begin
Sleep(100);
Synchronize(doMethod);
end;end;procedure TForm1.test;
begin
Memo1.Lines.Add(FormatDateTime('hh:NN:ss.zzz', now))
end;procedure TForm1.Button1Click(Sender: TObject);
begin
FmyThread := TtestThread.Create(self);
Button1.Enabled := false;
FmyThread.Resume;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
FmyThread := nil;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if FmyThread <> nil then
begin FmyThread.Terminate; end;
end;end.
你调用
Synchronize(doMethod);
是必须等待doMethod执行完毕才释放控制权,要不就一致等待,所以就产生界面死了的现象.
把Synchronize去掉,直接调用doMethod.