Tried to run Windows GUI program, but a while loop is blocking event loop in C++

  • Comp Sci
  • Thread starter Pipsqueakalchemist
  • Start date
  • #1
Pipsqueakalchemist
138
18
Homework Statement
Trying to display window GUI but apparently loop blocking event loop
Relevant Equations
C++, visual studio
So I've just recently learned what a event loop is and the concept of blocking it. My question is I've written loops before in a console (main()) program and this never happened. Is there something about a GUI program (WinMain()) that makes loops block the event loop?

Also I understand that during the loop, the event loop is forced to wait until the loop is complete and can't execute other task. But if the program is currently in the loop, wouldn't the program encounter the code, tasks that are inside of the loop and be able to execute them?

For example i tried to display a window i made and i put it into a while(1) loop, i had a cin inside the loop so the idea was to keep displaying the window until a i pressed 's' then break out of the loop hence closing the window. But the program was unresponsive. Shouldn't the program encounter my cin because my cin code is inside the loop. i have a picture of my code below.

Appreciate any insight.

image.png
 
Physics news on Phys.org
  • #2
Pipsqueakalchemist said:
So I've just recently learned what a event loop is and the concept of blocking it.
I'm not sure why you want to block the event loop. It's supposed to run forever -- that's the reason for it being a "while(1)" type of loop.

Your if statement inside your while loop doesn't "block" the loop. If the current value of stop is 's', the break statement exits the loop.
Pipsqueakalchemist said:
Also I understand that during the loop, the event loop is forced to wait until the loop is complete and can't execute other task. But if the program is currently in the loop, wouldn't the program encounter the code, tasks that are inside of the loop and be able to execute them?
It's not clear to me what you're saying here. Your event loop, which includes a call to ShowWindow(), is supposed to run forever; that is, until the user enters the character 's'. If any other character is entered, the event loop continues running.
Pipsqueakalchemist said:
For example i tried to display a window i made and i put it into a while(1) loop, i had a cin inside the loop so the idea was to keep displaying the window until a i pressed 's' then break out of the loop hence closing the window. But the program was unresponsive. Shouldn't the program encounter my cin because my cin code is inside the loop. i have a picture of my code below.
Yes, once you enter 's' that should cause your loop to exit. You should use the VS debugger to see why your program isn't behaving the way you think it should.
 
Last edited:
  • #3
Pipsqueakalchemist said:
So I've just recently learned what a event loop is and the concept of blocking it.
In addition to what Mark already asked, I'd like offer a bit of general background information since you seem to talk about both the main event loop and "your own" loops in a confusing mix.

Once a (GUI) program is started the event loop is the main framework provided loop (i.e. not a piece of code that you provide) that picks up input events from various GUI elements and dispatches the to the associated logic provided by framework or application programmer. For instance, when the user clicks a button with an associated action to load a file this will make the loop call that action synchronously from the GUI event thread meaning any code running in that action will block the event loop for as long as it runs. A long running synchronous actions (e.g. loading a file that takes time) will leave the UI frozen and unresponsive until that action has completed, unless special care is used like running long running tasks in another thread (or as a task), and then fire another event when completed to update the GUI.

Some GUI frameworks also offers the single-threaded option of allowing long running actions the ability to call back to the framework to process any events at regular interval, usually until all pending events have been processed. For instance, a file reader action could call this event processing after reading each chunk of data so that the application is still is somewhat responsive and not completely frozen even though the file in total may take a long time to load. Such a local (programmer provided) event loop would then be a good place to handle any file loading dialog, possibly updating progress bar and checking cancel button presses.
 
  • #4
According to the language standard, What is the range of that "Break"?

If it is the immediately surrounding "{...}" then it is probably operating correctly.
 

FAQ: Tried to run Windows GUI program, but a while loop is blocking event loop in C++

Why is my Windows GUI program not responding when I use a while loop?

When you use a while loop in your Windows GUI program, it can block the event loop, preventing the program from processing other events such as user inputs or window updates. This makes the application unresponsive.

How can I prevent a while loop from blocking the event loop in my Windows GUI program?

To prevent a while loop from blocking the event loop, you can use techniques such as running the loop in a separate thread, using a timer to periodically execute code, or integrating the loop with the event loop by checking and processing events within the loop.

What is the event loop in a Windows GUI program?

The event loop in a Windows GUI program is a loop that continuously checks for and processes events such as mouse clicks, keyboard input, and window messages. It ensures that the application remains responsive to user interactions.

Can I use multithreading to handle long-running tasks in a Windows GUI program?

Yes, you can use multithreading to handle long-running tasks in a Windows GUI program. By running the long-running task in a separate thread, you can keep the main thread free to process events, ensuring the application remains responsive.

What are some alternative methods to handle repetitive tasks without blocking the event loop?

Some alternative methods to handle repetitive tasks without blocking the event loop include using timers (such as SetTimer and KillTimer in the Windows API), utilizing asynchronous programming techniques, or leveraging task-based parallelism provided by libraries like the C++ Standard Library's std::async or third-party libraries like Boost.Asio.

Similar threads

Replies
10
Views
2K
Replies
10
Views
2K
Replies
2
Views
8K
Replies
6
Views
4K
Replies
5
Views
2K
Back
Top