Solved

Show Modal form without stopping the program

Posted on 1998-09-11
4
1,141 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 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