Recent

Author Topic: [SOLVED] May Event ButtonClick() be called twice (at the same time)?  (Read 328 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1000
I plan a Form with 6 buttons. They all shall use the same ButtonClick() Event.

When buttons 1-3 are pressed, all 3 buttons are disabled (so they can't be pressed again) and a corresponding longer running task is started, which may call Application.ProcessMessages(). This would allow, that buttons 4-6 can be pressed, which start only very short tasks, where Application.ProcessMessages() can not be called. When a longer running task is finished, buttons 1-3 are enabled again.

From my understanding this concept would mean, that Event ButtonClick() could be entered a 2nd time (by buttons 4-6), while the 1st call to this ButtonClick() Event (by buttons 1-3) has not yet returned, as long as it's corresponding longer running task has not finished.

My question is not, if this concept would win a price for very good programming style.
My question is, whether any real trouble might occur doing so.

If the answer would be yes, one normaly would "move" the start of a corresponding longer running task via Application.QueueAsyncCall(). But then the longer running task would be started, when the Application "is idle" the next time, but I had no (or very difficult) control about this point in time.

That's why I'm asking, whether any real trouble might occur entering Event ButtonClick() twice.
Thanks in advance.
« Last Edit: October 06, 2025, 07:27:25 pm by Hartmut »

LeP

  • Jr. Member
  • **
  • Posts: 57
Re: May Event ButtonClick() be called twice (at the same time)?
« Reply #1 on: October 06, 2025, 05:02:09 pm »
There is no issue to execute (or allow executing) any event two times (without waiting the end of previous elaboration).

But you must care all the global variables: if they are changed by the event procedure you don't know wich "wins". Don't take the assumption that the first execution will end "first".....

In general, to do long time activities you should use other techs like multithreading, use one or more thread to do the long works and leave the UI waiting for the end of the thread(s) for example by synchornize call or callback from threads.

P.S.: also note the Application.ProcessMessages() may have side effects (like the one you noticed, but not only).

« Last Edit: October 06, 2025, 05:08:42 pm by LeP »

Hartmut

  • Hero Member
  • *****
  • Posts: 1000
Re: May Event ButtonClick() be called twice (at the same time)?
« Reply #2 on: October 06, 2025, 06:55:18 pm »
Thanks a lot LeP for those informations.

There is no issue to execute (or allow executing) any event two times (without waiting the end of previous elaboration).
That's very good news.

Quote
But you must care all the global variables
yes, I should not forget

Quote
In general, to do long time activities you should use other techs like multithreading
In my case the longer running tasks do a lot of output e.g. to a TMemo, so multithreading would be unnecessarily complicated and laborious.

Quote
also note the Application.ProcessMessages() may have side effects (like the one you noticed, but not only).
What do you mean with "but not only"? Can you please be more detailed?

LeP

  • Jr. Member
  • **
  • Posts: 57
Re: May Event ButtonClick() be called twice (at the same time)?
« Reply #3 on: October 06, 2025, 07:13:27 pm »
Application.ProcessMessages forces the Windows "message loop" of you app to execute all pending messages. These means that all messages are processed: system, other threads, other actions.

Force those without thinks about may come with a surprise at runtime.

Think about one press <ALT> <F4> during you works ....

Hartmut

  • Hero Member
  • *****
  • Posts: 1000
Re: May Event ButtonClick() be called twice (at the same time)?
« Reply #4 on: October 06, 2025, 07:27:00 pm »
Thanks LeP for clarification.

 

TinyPortal © 2005-2018