How to wait for buttonclick in loop?

Stellaferox

Active Member
Licensed User
Hi,

I cannot seem to figure out how to wait for a buttonclick in a masterloop. A have this masterloop and on a certain moment I need a buttonclick on one (of 14) buttons and pass the sender to a subroutine to process.
How do I code for the miniloop in which to wait for the buttonclick? Without this button the masterloop mustnot continue.
thnx in advance
Marc
 

Stellaferox

Active Member
Licensed User
Yes Ariel, thnx, I know. I use this Click event for a subroutine which follows but how can I enforce the waiting for that clicking (and passing the Sender command) for that event. Is there a listening command for a buttonclick?
Marc
 

Stellaferox

Active Member
Licensed User
Yes Agraham, the subroutine is waiting for the click. I need to point out a button in a masterloop without which the masterloop cannot continue. I am starting a sequence, must then push a button and then the loop continues.
The problem is how can I create a miniloop which waits for a buttonclick. I tried DoEvents but this can generate any event.
 

agraham

Expert
Licensed User
Longtime User
The problem is how can I create a miniloop which waits for a buttonclick.
You shouldn't have wait loops in an event driven environment. As you say, DoEvents() allows any events which is why it is dangerous unless you code for the possibility and so should normally be avoided. If your application really must wait for this, and only this, single event to happen you should code it accordingly, For example by disabling all the other controls so that their events cannot occur and/or by having one or more global variables that you use as flags and check in every event to see if that event should be processed or not. A combination of these two, depending upon your actual code, will probably be neatest.
 

klaus

Expert
Licensed User
Longtime User
Sorry, but I don't understand your problem either.

What is your main loop dooing ?
Why using this main loop, aren't the normal events sufficient ?
You could set flags to manage different options in your routines.

Your question reminds some old days, before event driven OS's, when we had to manage a main loop waiting for user inputs to call the requested routines and having also some subloops to manage submenus and so on.

Could you post some code that we could have a closer look at, and better understand your needs.

Best regards.
 

Stellaferox

Active Member
Licensed User
Agraham, Klaus, thnx.
Yes I am from the old days when we had to code in one giant loop but this puzzled me for a while.
Eventually I did as mentionede before. DoEvents and disable the other controls.
thnx
Marc
 

agraham

Expert
Licensed User
Longtime User
Yes I am from the old days when we had to code in one giant loop
So am I (the first computers I worked were built using individual germanium transistors!), making the change to an event-driven environment can take a while.
DoEvents and disable the other controls.
It sounds like you still have a loop in there which is not really a good thing. If you have protected things well enough for DoEvents to be safe you shouldn't need the loop as the only events that will be actioned are the ones you want so you can just let your code return and wait.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I think I understand exactly what you are struggling with as when I first started using an event driven language I found it a little difficult to comprehend.
But as Klaus has already pointed out, I think that what you need to do is set a flag. By this we mean that when you click the button a global variable is set. For example at the start of the program set your variable eg ButtonClicked = False then when the click event is fired just set the varaible to ButtonClicked = True.
The other sub will then check this variable, this could be done periodically using a timed event which if false does nothing but if true then calls the rest of your program. Sitting in a continuous loop is bad as it eats up resources and consumes battery life on mobile devices.

Hope this helps to clear any confusion.
Regards,
RandomCoder
 
Top