0

I have to design a program that uses a main loop to keep track of time and to perform continuous checks. Also I have a gui system that can alter things in the system. The question is:

If i run a continuous loop that doesnt stop until the system exits, will the user be able to interact with the GUI I have set up, or will the loop have to stop running for the effect of pressing a button to take place? In other words, do i have to pause the loop, run the button command, then restart the loop, everytime a user interacts with the GUI? and if so, is there a way around this? Thanks to any and all who answer

1
  • Which GUI framework/toolkit/library are you using? It's a little bit different in each, but basically there's usually a function to call that starts the GUI monitoring user actions, either in a separate thread or the main thread you call it from. You can either arrange periodic or event-based callbacks or have started your own thread beforehand. Commented Feb 26, 2011 at 20:10

2 Answers 2

2

Since you didn't state your platform, I will term my reply generically.

There are two patterns you can use: polling or interrupt / event driven.

Polling

The polling involves checking a semaphore or flag frequently to see if something happened. This is common in embedded systems. A background loops until an interrupt sets a flag then processes the event.

Interrupt / Event Driven.

In this pattern, a function is executed when an event occurs. For example, a function may be executed when a User clicks on the button.

On Desktop platforms (Mac, Linux, Windows, etc.), your situation is resolved by using multiple threads (of execution). Usually the GUI operation is on one thread and the main processing on another. This allows the program not to freeze the GUI when a button is clicked.

On my application, the GUI sets an event when the user clicks a button. The processing thread waits (sleeps, pends) on the event. The User clicks the button, the event is set. The processing thread wakes up and continues.

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

Comments

1

It seems that you are trying to do DOS-like programming in Windows which works the other way around. You should read something about Event Driven Programming. There should not be any main loop in your application. All interactions should be done through events. When user press the button - an event is fired. Instead of the loop you are describing you should create a timer that will execute a method that will do the necessary checks.

Hope this helped a bit.

5 Comments

can a time cycle qualify as an event? I mean, is there a way to cause an interrupt at a regular interval that performs these checks?
@Mark - Generally, it is possible that you will end implementing things that other already did.
@Mark: Yes, these are called timers. Supply the timer with a callback function and a duration. Let the OS decrement the timer then call your function when the duration has expired.
@Mark: Jenea says "there should not by any main loop in your app", but actually any GUI app will have a main loop. Usually, though, you won't write this loop. You'll just invoke a main loop routine provided by your GUI toolkit. That way, your toolkit can keep the app "alive": responding to events and firing timers. Then, as @Thomas said, you can do your work without getting in the toolkit's way, by creating a timer and doing only small amounts of work every time the timer fires.
@Mark: Sometimes this means you should split your task into chunks and process those incrementally over over several GUI cycles. In the rare case your app needs to do large, monolithic chunks of work, use a thread instead, so you don't block the main loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.