To thread or not too thread?

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
Dave McConnellRetiredAsked:
Who is Participating?
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
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
 
BJThomsenCommented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
Dave McConnellRetiredAuthor Commented:
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
 
Dave McConnellRetiredAuthor Commented:
Thanks so much for helping me with what was probably a very simple answer, but did not appear that way to me....

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.