Solved

Show Modal form without stopping the program

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Save pdf file to other location 3 72
Path  to current project in Delphi. 2 67
Simple Delphi Question 9 77
Magic Software info 18 105
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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

9 Experts available now in Live!

Get 1:1 Help Now