Recent

Author Topic: [SOLVED] Problem accessing GUI element from Thread on Modal form  (Read 6843 times)

mudah

  • New Member
  • *
  • Posts: 13
Hello

I need some help here. I have created Child Form as modal and now having problem accessing GUI element from within TMyThread.ShowStatus. Below is the complete code..

Parent form

Code: [Select]
unit uParent;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  uChild;

type

  { TfParent }

  TfParent = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  fParent: TfParent;

implementation

{$R *.lfm}

{ TfParent }

procedure TfParent.Button1Click(Sender: TObject);
var
    OpenChild: TfChild;
begin
    OpenChild:= TfChild.Create(nil);
    OpenChild.ShowModal;
    ShowMessage('Child Closed');
end;

end.

Child Form
Code: [Select]
unit uChild;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, StdCtrls;

type

  { TMyThread }

  TMyThread = class(TThread)
  private
    fStatusText: string;
    procedure ShowStatus;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: boolean);
  end;

  TfChild = class(TForm)
      Button1: TButton;
      Label1: TLabel;
      procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;


var
  fChild: TfChild;

implementation

{$R *.lfm}

{ TfChild }

procedure TfChild.Button1Click(Sender: TObject);
var
      MyThread : TMyThread;
 begin
      MyThread := TMyThread.Create(True);
      MyThread.Resume;
 end;

procedure TMyThread.ShowStatus;
begin
    fChild.Caption := fStatusText; //<---- problem here!

end;

procedure TMyThread.Execute;
begin
fStatusText := 'Starting';
    Synchronize(@Showstatus);
end;

constructor TMyThread.Create(CreateSuspended: boolean);
begin
  FreeOnTerminate := True;
  inherited Create(CreateSuspended);
end;
end.

Herewith attached source code. Can someone help me please :'(

Thanks a bunch!

-- WinXP SP3
-- Lazarus 0.9.30.1 - 31909
-- FPC 2.4.2
« Last Edit: August 11, 2011, 04:25:48 am by mudah »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Problem accessing GUI element from Thread on Modal form
« Reply #1 on: August 10, 2011, 11:37:15 pm »
Man, I'll kill you.  :D :D :D

I was thinking so long and it was in front of my eyes.

Of course, you must have openChild:

Code: [Select]
procedure TMyThread.ShowStatus;
begin
   openChild.Caption := fStatusText;

end;

because openchild is created when you press fParent.button1 .
And, of course, you must move openchild to global variables.
Or, create (or swohmodal)  fChild, when fParent.Button1 click.

(Note: I didn't use your original project; I only copy/pasted the code, so I don't know if your fChild is autocreated or not)

EDIT: Resume seems be deprecated. See here: http://wiki.freepascal.org/User_Changes_2.4.4#TThread.Suspend_and_TThread.Resume_have_been_deprecated
« Last Edit: August 10, 2011, 11:50:45 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mudah

  • New Member
  • *
  • Posts: 13
Re: Problem accessing GUI element from Thread on Modal form
« Reply #2 on: August 11, 2011, 04:25:13 am »
Global variable.. rite.  I commit suicide now.. :-[

Problem solved by moving openChild to global variable.



Thanks a bunch Blaazen!

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: [SOLVED] Problem accessing GUI element from Thread on Modal form
« Reply #3 on: August 11, 2011, 10:57:11 am »
I forgot one think:
Code: [Select]
procedure TfParent.Button1Click(Sender: TObject);
begin
    OpenChild:= TfChild.Create(nil);
    OpenChild.ShowModal;
    ShowMessage('Child Closed');
    OpenChild.Free;  // <--- HERE
end;

You should can Free to avoid memory leaks.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mudah

  • New Member
  • *
  • Posts: 13
Re: [SOLVED] Problem accessing GUI element from Thread on Modal form
« Reply #4 on: August 22, 2011, 07:17:01 pm »
+1

Thanks again Blaazen!


TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Re: [SOLVED] Problem accessing GUI element from Thread on Modal form
« Reply #5 on: August 22, 2011, 09:01:32 pm »
http://lazarus.freepascal.org/index.php/topic,14321.msg75754.html#msg75754

Quote
do not forget that which you create you must also destroy

Quote
Ok :) I write Destroy/destructor code...
Oké :) Írok lezáró részeket (Destroy)...
« Last Edit: August 22, 2011, 09:08:55 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

 

TinyPortal © 2005-2018