Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

MDI Child Window Question

I have a MDI Application each child window that is launched has to access a database, write a html page and display that page in a TWebBrowser control.

I put the code to do all of that into a thread. The thread isn't in it's own unit I just defined it in the same unit as the MDI Child window.

I have a timer control that runs every 5 seconds.

The thread doesn't appear to be running. Is it possible to run a thread from inside a MDI Child window?
0
ramonecarson
Asked:
ramonecarson
  • 6
  • 4
  • 2
  • +2
2 Solutions
 
ramonecarsonAuthor Commented:
Can anyone show me some code to do this? Or point me to an article that explains threading from inside an MDI Child form.

I wouldn't think it would be any different but I can't imagine what I'm doing wrong here.
0
 
classmateCommented:
Is this a general thread question or more a MDI usage question?

regards,
classmate.
0
 
ramonecarsonAuthor Commented:
Classmate,

I'm not sure what it is. I know how to use threads, I know how to make an MDI application. I have another thread object in this application that the MDIParent form uses.

But when I do the same thing in the MDIChild it no worky.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
classmateCommented:
1) What is the connection between your timer and the thread(s).
2) What is the connection between your thread(s) and the MDI forms; is there one thread that generate the forms or is there one thread per form, or something else.
3) What is the purpose of your thread(s), and in what ways do they eventually use common resources.
4) From what point are the thread(s) executed.
5) Do you absolutely need to use threads for your task.

classmate.
0
 
ramonecarsonAuthor Commented:
On the Parent Form there is a thread that has a socket open that it reads data from.

if new data comes in and it's for one of the MDI Children the Parent form sets the Childs timer control to enabled

Inside the MDI Child the timer checks to see if the thread is already running. If it is it just exits and sets enabled state to false.

The purpose of the threads is they access a database get information out of it. Create a web page based on that data and update the TWebBrowser Control.

Right now I have the code to do that in the timer control and although it's not a big deal the MDI Child freezes while the page is being updated. Which is 10 seconds at most.

The whole process works until I place the code to make the page in a thread and try to either create a thread or resume a thread from that timer control
0
 
j42Commented:
Hi,

just some thoughts:
- the Execute method of the Delphi TThread object dies silently in case of an exception (D1 - D5, not sure about D6, D7)
- Accessing visual components in the Execute method will lead to AV

Regards
0
 
classmateCommented:
My first consideration is that it seems like you perform page loading and visual updating in one operation, since it takes 10 secs. If you can manage to split this up by loading html into a buffer using the thread, and then display it in the Twebbrowser using the main thread (the main program loop), you might become able to move mdi windows while loading.
As far as i know, it's not a very good idea to update visual components within any other thread than the main thread, unless you know EXACTLY what you are doing.
A way to come around this easily is to use the Synchronize method, which calls the main thread from other threads.

This approach assumes that the process of loading data is what freezes your forms.
0
 
ramonecarsonAuthor Commented:
I am using the Synchronize method to update the Visual Controls and that's not my problem. My problem is that my MDI Child Forms are executing the thread. I made the form into a regular form and the code works.

Any more thoughts?

I will look to see if an exception is occurring.
0
 
j42Commented:
> Is it possible to run a thread from inside a MDI Child window?
> My problem is that my MDI Child Forms are executing the thread

Can I get some more explanation? I am not sure about what you do.
0
 
classmateCommented:
Strange...

I tried this test in a standard mdi project.
The form contains a memo, which gets a new "hours-line" each second. This works for me.

Classmate.



///*****************************************************
unit CHILDWIN;

interface

uses Windows, Classes, Graphics, Forms, Controls, StdCtrls, SysUtils;

type
  TMDIChild = class;

  TClockThread = class(TThread)
    FForm : TMDIChild;
    FTime : Integer;
    procedure Execute; override;
    procedure ShowTime;
  end;

  TMDIChild = class(TForm)
    Memo1: TMemo;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FClock : TClockThread;
  public
    { Public declarations }
    constructor create (aowner: TComponent); override;
    destructor destroy; override;
  end;

implementation

{$R *.dfm}

procedure TMDIChild.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;


constructor TMDIChild.create (aowner:TComponent);
begin
  inherited;
  FClock := TClockThread.Create(true);
  FClock.FreeOnTerminate := false;
  FClock.FForm := self;
  FClock.Priority := tpLowest;
  FClock.Resume;
end;

destructor TMDIChild.destroy;
begin
  FClock.Priority := tpHighest;
  FClock.terminate;
  FClock.WaitFor;
  FClock.Free;
  inherited;
end;


procedure TClockThread.Execute;
var
  t : integer;
begin
  FTime := GetTickCount;
  Synchronize (ShowTime);
  while not terminated do begin
    t:= GetTickCount;
    if t-FTime > 1000 then begin
      FTime := GetTickCount;
      Synchronize (ShowTime);
    end;
  end;
end;


procedure TClockThread.ShowTime;
begin
  if FForm.Memo1.HandleAllocated then
    FForm.Memo1.Lines.Insert(0, TimeToStr (Now));
end;




end.
0
 
classmateCommented:
Maybe no handle is allocated to TWebBrowser before it is accessed? In that case, synchronize might not be enough...

classmate.
0
 
classmateCommented:
Alternative approach, posting messages instead of using synchronize, making the processor less occupied at the time when a user is interacting.
Drawback: Terminating this application takes longer time.



unit CHILDWIN;

interface

uses Windows, Classes, Graphics, Forms, Controls, StdCtrls, SysUtils,
     messages;


Const
  PM_INSERTTIME = WM_USER + 500;

type
  TMDIChild = class;

  TClockThread = class(TThread)
    FForm : TMDIChild;
    FTime : Integer;
    procedure Execute; override;
    procedure ShowTime;
  end;


  TMDIChild = class(TForm)
    Memo1: TMemo;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FClock : TClockThread;
    procedure PMInsertTime (var message : TMessage); message PM_INSERTTIME;
  public
    { Public declarations }
    constructor create (aowner: TComponent); override;
    destructor destroy; override;

  end;

implementation

{$R *.dfm}

procedure TMDIChild.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;


constructor TMDIChild.create (aowner:TComponent);
begin
  inherited;
  FClock := TClockThread.Create(true);
  FClock.FreeOnTerminate := false;
  FClock.FForm := self;
  FClock.Priority := tpLowest;
  FClock.Resume;
end;

procedure TMDIChild.PMInsertTime (var message : TMessage);
begin
  if Memo1.HandleAllocated then
    Memo1.Lines.Insert(0, TimeToStr (Now));
end;

destructor TMDIChild.destroy;
begin
  FClock.Priority := tpHighest;
  FClock.terminate;
  FClock.WaitFor;
  FClock.Free;
  inherited;
end;


procedure TClockThread.Execute;
var
  t : integer;
begin
  FTime := GetTickCount;
  PostMessage (FForm.Handle, PM_INSERTTIME, 0, 0);
  while not terminated do begin
    t:= GetTickCount;
    if t-FTime > 1000 then begin
      FTime := GetTickCount;
      PostMessage (FForm.Handle, PM_INSERTTIME, 0, 0);
    end;
  end;
end;


procedure TClockThread.ShowTime;
begin
  if FForm.Memo1.HandleAllocated then
    FForm.Memo1.Lines.Insert(0, TimeToStr (Now));
end;




end.
0
 
CleanupPingCommented:
ramonecarson:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
snehanshuCommented:
Hi!
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:

Split between classmate and j42

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

...Snehanshu
EE Cleanup Volunteer
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now