0

If I use delphi ShowMessage or MessageDlg when the message is shown the program wait the click in the button and I wouldn't like that the program blocks himself. I need to show a message where I write that the program is searching, but as long as the message is shown the search doesn't start.. What can I do?

Thanks, Jack

ShowMessage('Sto ricercando . . .');
if (cartellaSorgente[Length(cartellaSorgente)] <> '\') then
begin
// do the research
end;
Jack1995
  • 9
  • 3
  • Please post your code as Crystal Ball is on holiday today... – ElmoVanKielmo Jul 15 '13 at 10:11
  • I dare to say that you are going to run into other problems ("My application seems to freeze while I am searching for stuff", ...) if you use a non-blocking messagebox and want the searching to take place in the main thread. You should have a look at the `TThread` class to create a second thread that handles your searching and finally adds the result to some object in your main thread before it finally terminates. – Günther the Beautiful Jul 15 '13 at 10:12
  • If you need to use ShowMessage or MessageDLG you could get a inspiration from here : http://stackoverflow.com/questions/15696885/why-does-a-messagebox-not-block-the-application-on-a-synchronized-thread – bummi Jul 15 '13 at 10:12
  • Read about OmniThreadingLibrary - they have a good articles, wiki-book, blog, and forum – Arioch 'The Jul 15 '13 at 11:35

1 Answers1

5

ShowMessage displays a modal dialog. This means that the call to ShowMessage does not return until the dialog is closed.

Your problem is that you wish to execute a long-running task without blocking the UI. The way to achieve that is to put the long-running task, the search, into a separate thread. Send messages from the search thread to the UI thread to allow the UI to inform the user of progress.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490