?
Solved

To thread or not too thread?

Posted on 2005-04-19
5
Medium Priority
?
214 Views
Last Modified: 2010-04-05
very simply I have an application which has many forms.  Wiithin the mainform I would like to run a timer that executes every 10 minutes.  While executing I may need to call another form and perform some process before returning to the mainform and it's running loop.  Is there an elegant way to "pause" execution, create form and setfocus to other form where the user can then process some data.  When the user closes the opened form then it would resume execution in the mainform.  I just wondered if I will need to investigage threading at lenght to accomplish this or possibly Delphi was kind enough to include some functionality inherent in Enterprise version 6.

Thanks for everyone's insight.....

-Dave
0
Comment
Question by:Dave McConnell
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:BJThomsen
ID: 13820723
Not sure I understand the question but

... do some stuff

with form.create(nil) do
begin
  Showmodal;
  Free;
end;

... after user closes form, do some more stuff.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 13820831
typically in a TTimer.OnTimer event you'll handle it like this

procedure TForm1.Timer1Timer(Sender:TObject);
begin
    Timer1.Enabled := False; // stop the timer while we do the work
    try
// do stuff here, for example open your other form to get some data from teh user
        with TForm2.Create(self) do
        try
            if SHowModal = mrOK then
            begin
                // user pressed ok
            end;
        finally
            free;
        end;
    finally
        timer1.enabled := true; // turn the timer back on now
    end;
end;
0
 

Author Comment

by:Dave McConnell
ID: 13832636
I am not sure why the comment I posted yesterday did not dsiplay, but here it is again.

I am using forms which have already be designed in the ide and so I usually call them with something like...

Application.CreateForm(TMarylandForm, MarylandForm);
MarylandForm.Left := ((Screen.Width - MarylandForm.Width) div 2) + 10;
MarylandForm.Top := ((Screen.Height - MarylandForm.Height) div 2) + 40;
MarylandForm.Visible := True;
MarylandForm.InternetMemo.Visible := True;
MarylandForm.InternetButton.Visible := True;
MarylandForm.InternetButton.SetFocus;


When I tried either approach to create the form I received runtime errors when executing either method of opening the form.  The message was "exception class EComponentError with message "A component named CallerLabel already exists".  I am assuming that when the form is created with either TForm2.Create(self) or TForm2.Create(nil) the form must not previously exist anywhere.

Anyway, I still need help with this issue...

Thanks,
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 1200 total points
ID: 13837180
is your 2nd form being created in the dpr?  Alt-Project-Options-Auto Create forms

you can still do it your way, as long as you have it created only once, and then just call
if MarylandForm.ShowModal = mrOK;  //showmodal waits until the form has closed before continuing

if you wanted to do the more dynamic with TMarylandForm.create do try... showmodal.. finally free; end  method then you should make sure the form isn't auto-created in the dpr, and you will not use the MarylandForm variable at all
it would be like :-

with TMarylandForm.create(self) do
try
    Left := ((Screen.Width -Width) div 2) + 10;
    Top := ((Screen.Height - Height) div 2) + 40;
//    MarylandForm.Visible := True; // you do not use this, you should be using "Show" instead
    InternetMemo.Visible := True;
    InternetButton.Visible := True;
    InternetButton.SetFocus;
    If ShowModal = mrOK then
    begin
// user pressed ok, get the result
        showmessage(Internetmemo.lines.text);
    end;
finally
    free;
end;

If you want to have the same MarylandForm, but keep showing it in different circumstances, then you would have it created in teh dpr (as normal) and then
with MarylandForm do
try
    Left := ((Screen.Width -Width) div 2) + 10;
    Top := ((Screen.Height - Height) div 2) + 40;
//    MarylandForm.Visible := True; // you do not use this, you should be using "Show" instead
    InternetMemo.Visible := True;
    InternetButton.Visible := True;
    InternetButton.SetFocus;
    If ShowModal = mrOK then
    begin
// user pressed ok, get the result
        showmessage(Internetmemo.lines.text);
    end;
finally
    free;
end;

0
 

Author Comment

by:Dave McConnell
ID: 13842315
Thanks so much for helping me with what was probably a very simple answer, but did not appear that way to me....

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

862 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