?
Solved

MDI Child Window Question

Posted on 2003-03-05
15
Medium Priority
?
489 Views
Last Modified: 2010-04-06
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
Comment
Question by:ramonecarson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +2
15 Comments
 

Author Comment

by:ramonecarson
ID: 8073023
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
 
LVL 2

Expert Comment

by:classmate
ID: 8073550
Is this a general thread question or more a MDI usage question?

regards,
classmate.
0
 

Author Comment

by:ramonecarson
ID: 8073582
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
Independent Software Vendors: 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!

 
LVL 2

Expert Comment

by:classmate
ID: 8074466
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
 

Author Comment

by:ramonecarson
ID: 8074952
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
 
LVL 2

Assisted Solution

by:j42
j42 earned 400 total points
ID: 8078409
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
 
LVL 2

Expert Comment

by:classmate
ID: 8079073
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
 

Author Comment

by:ramonecarson
ID: 8083191
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
 
LVL 2

Expert Comment

by:j42
ID: 8087247
> 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
 
LVL 2

Expert Comment

by:classmate
ID: 8101948
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
 
LVL 2

Expert Comment

by:classmate
ID: 8101968
Maybe no handle is allocated to TWebBrowser before it is accessed? In that case, synchronize might not be enough...

classmate.
0
 
LVL 2

Accepted Solution

by:
classmate earned 400 total points
ID: 8102042
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
 

Expert Comment

by:CleanupPing
ID: 9316928
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 10037288
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question