2012/11/19 Eric Kom <[email protected]>: > Thanks for all Martin, it's works > > > StatusBar.SimpleText := 'My name is Tux'; > StatusBar.Update; > sleep(2000); > StatusBar.SimpleText := 'am ready'; > > I will try the hard code also
You should generally try your best to avoid using Sleep() in the above way in most cases. If you have a very simple sequence of waiting times (in your example you have only one) you can easily get away with simply placing a TTimer onto your form and using that to generate a *second* event when the 2 seconds have passed. Using your example with the status bar text in the first event you would set the statusbar text to 'My name is Tux', then start the timer and let your method return. In the timer event set the status bar text to 'am ready' and then also immediately return. During the 2 seconds between these two events the GUI will stay fully functional because nothing is blocking the main thread and other events are able to fire as needed. The more complicated solution would be to start a thread in the first event and let the thread do the sleep (or the long running task) but note that it is absolutely *not* allowed to directly call any LCL methods from within this worker thread (this makes it slightly more complicated) you would need to use Synchronize() or Application.QueueAsyncCall() to notify the main thread. The general idea is to make sure all your event methods will return as fast as possible because all the GUI is running from within only one thread (the main thread) and this is basically the only thread that should *never* sleep or block or do long computations. As long as one of your event methods is wasting time in Sleep() (or doing other long running things) the entire GUI will be frozen during this time, this will give a very bad user experience, no other events will be able to fire during this time, no resize, no screen update (this is what you observed), not even closing the form will be possible. There are some concepts you should study, they are not restricted to Lazarus, most GUI frameworks use the event driven concept, so even if you understand how it works (or how you would do it) for example in C++/Qt or Java/Swing or in GTK or in wxWidgets or basically almost any other framework then you can apply the same knowledge to FPC/Lazarus. Only the Methods will have different names but the problems and their solutions are the same. Try to find examples, discussions, etc. about the following topics: "event driven programming" "TTimer" "worker threads" "TThread" and "TThread.Synchronize" "TApplication.QueueAsyncCall" And if you are doing networking (I am inferring this from the name of your method) I strongly recommend using the LNet components, LNet has an extremely neat architecture and a fully asynchronous event driven model (no sleeping, no blocking ever) that fits nicely into the rest of LCL. Unfortunately it is not very extensively documented but on the other hand it is very small (only ~1000 lines without the optional protocol units) and its actually quite easy to use. -- _______________________________________________ Lazarus mailing list [email protected] http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
