0

There are 2 forms.

Form2 isn't auto-created.

 Form2:=TForm2.Create(Application); Form2.Show; 

If to do Sleep(10000); inside any forms then another one will be frozen. How to prevent this behavior?

My problem: a big text file (3 Mb) is continuously assigned (Lines.Assign) into a text editor, so a form is frozen.

Can I use another unfrozen form (not modal) to show a progress bar (a style is pbstMarquee)?

5
  • The very DEFINITION of the Sleep() function is "freeze this thread", and if you freeze the main thread, you freeze all your forms, because they all work only from the main thread. Sounds like you need to understand the importance of message-pumps in win32 application design. en.wikipedia.org/wiki/Event_loop Commented Aug 11, 2011 at 14:37
  • Is there ever a real reason to call Sleep? Commented Aug 11, 2011 at 17:11
  • In the foreground thread? No. And even in a background thread, WaitForSingleObject would be better. I think the OP was using the Sleep() to mock up how long his 3mb text file took to load. Commented Aug 11, 2011 at 18:27
  • maxfax; If you're going to load 3mb files, try using SynEdit instead of TMemo. Commented Aug 11, 2011 at 18:29
  • @Nick Hodges - sure. Many protocols, for example, require delays. WFSO reqires creating a synchro object to wait on and the only justification for this complexity is the near-hopeless thread support in Delphi, (yes, I know, other languages are as bad or not much better), that obliges developers to struggle to terminate background threads before freeing forms on app. close. Commented Aug 11, 2011 at 19:54

2 Answers 2

6

All GUI code should be run from the main thread, and it looks like you are following that rule.

If you call Sleep then the calling thread will not execute code until the timeout elapses. If you call Sleep from the main thread, then the message queue will not be pumped until the timeout elapses. Hence the entire app appears frozen.

Why does calling Sleep from one form affect another form? Because all GUI components are served from the single message queue of the main thread. Once you stop pumping that queue, all GUI components stop receiving queued messages like WM_PAINT, WM_KEYDOWN etc.

As I understand it your problem is that your application appears hung when you are loading a 3MB text file into an edit control. That size of file doesn't sound very large to me and one obvious solution would be to find an edit control that performs the load better. For example I'm pretty sure that Notepad, Notepad++ etc. do not take steps like showing progress when loading such files. I rather suspect that those apps don't pump the queue when loading files but you just don't notice because of the short time taken.

What you don't want to happen is for you to pump your queue to keep your GUI responsive and in turn allow the user to start loading another file whilst the first one is still loading. You need to disable your UI whilst processing a load operation. A modal progress dialog is one way to do that.

If you can't switch to a better performing control, you could show a modal progress dialog and use a background thread like this. The background thread would have load the file in small chunks, say into a string list. After each chunk of the file was ready it would then call Synchronize and get the main thread to add the contents of the string list to the edit control, and then clear the string list. The thread would then continue and load the next chunk. Adding to the edit control in small chunks would allow you to keep the message queue serviced.

You could show progress in a status bar rather than a modal dialog which would be less intrusive. But just remember to disable any UI that would cause re-entrant execution.

Sign up to request clarification or add additional context in comments.

4 Comments

Notepad is very slow when loading large files. It does not process message and appears hung until loading is complete.
Alexey is right. Try to open 3 Mb with Notepad. Notepad++, as I suppose, doesn't load a whole file, it shows a part of the file to a user and load other when it is scrolled. I use SynEdit. How can I load chunks? If to add string by string it will be slowly. I need to load bytes by bytes :)
So it is possible to load and 100 Mb, but 100 Mb isn't really loaded. I think any program will be stuck if to load even 5 Mb continuously.
@Alexey I must admit I never use Notepad, I only ever touch Notepad++.
0

Better load your file in a separate thread. Or you will have to create your second form in a plain WinAPI, because VCL doesn't support multithreading.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.