我在主窗口中的一个按钮点击事件中创建了一个线程,线程调用了主窗体的其他按钮的点击事件,在退出程序的报错,Exception EOSError in…… 及“无效的窗口句柄”

解决方案 »

  1.   

    先释放线程,再退出程序,在线程开始设FreeOnTerminate=true试试
      

  2.   

    procedure test.Execute;
    begin
      Synchronize(Form1.method());
    end;
      

  3.   

    使用starsky2006的方法,我的函数根本没有被执行
      

  4.   

    你如果 不要 传复杂参数
    用message更好
      

  5.   

    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.
      

  6.   

    参照withcsharp() 的方法,退出程序时没有报错了,但是当程序执行的时候,不能点击鼠标,不然界面就失去响应了。怎样才让鼠标可以动呢?
      

  7.   

    线程不能调用界面事件驱动.
    你调用
    Synchronize(doMethod);
    是必须等待doMethod执行完毕才释放控制权,要不就一致等待,所以就产生界面死了的现象.
    把Synchronize去掉,直接调用doMethod.