Solved

Show Modal form without stopping the program

Posted on 1998-09-11
4
1,118 Views
Last Modified: 2012-05-04
I have an application in which there are several forms. One of them is a mail client to receive pop3 mail messages. My problem occurs when the messages are downloading, I like to let the user go to other forms. Some of these forms have modal dialogs. When one of these dialogs is opened, the download stops. What can I do to prevent that, threads maybe ? any example ?

Thank you.
0
Comment
Question by:sharons
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 30 total points
ID: 1339478
sharons,

it's quite complicated. I'll try to explain...
There is an important difference between form.Show and form.ShowModal. The first method shows the form and returns at once. The second method shows the form and does NOT return, but stays in a little loop like:
  while not Application.Terminated do
    Application.HandleMessage;
That means, all messages are processed, but Delphi stays in the ShowModal method.
I guess your download form has a little loop like the above one, too (perhaps Application.ProcessMessages instead of Application.HandleMessage). So probably this is happening:
Your download form downloads some bytes. Then it calls ProcessMessages. In ProcessMessages there comes a message that the user wants to open a modal form. So ProcessMessages calls ShowModal. But ShowModal doesn't return. So the ProcessMessages call in the download form's loop doesn't return either...

I hope you understand the problem right now. So what can you do to avoid such a situation? You suggested threads. But that won't help because you can't call something like ShowModal in a delphi thread, because Delphi's VCL isn't reentrant :-(. You could call ShowModal in the synchronize function of a delphi thread. But then the same sh... will happen like it happens now.

So my suggestion for you:
Change your download programming style. Delete the ProcessMessages loop. Instead of staying in that download function you should call it again and again, until the download is finished. There are three ways to do it like this:
(1) In the end of your download function you can post a message to yourself (e.g. WM_USER+77). Then you have to define a message handler for that message that just calls your download function again. So you can avoid that "message deadlock".
(2) Call your download function with a little timer.
(3) Use the Application.OnIdle event to call your download function.

Hope this helps...
Regards, Madshi.
0
 

Author Comment

by:sharons
ID: 1339479
Thank you Madshi.

What do you think about the following:

I have my main application. I call the method that downloads the email messages from within a separate thread. While the separate thread downloads the messages, the main application calls show modal.  Would the download be stoped then ?  Thank you.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1339480
sharons,

ok, variant number (4).   :-)

But be cautious with what you do in this thread, because (as I said before) the VCL is not reentrant (is it written this way?).
So changes in your download form (e.g. a progress bar) should be made with the thread.synchronize method.

Regards, Madshi.

P.S: Did you really understand my loooooooong garbage?
0
 

Author Comment

by:sharons
ID: 1339481
Thank you for your time. I have decided to limit the navigation while download occur.  I think it will be safer like that. The download functions are parts of components which I did not write myself. I prefer not to change them as a new release is coming up soon.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now