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
130
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.
 

1. Why is a while loop blocking the event loop when trying to run a Windows GUI program in C++?

While loops are typically used for continuous execution of a block of code until a certain condition is met. When a while loop is not properly managed in a Windows GUI program, it can block the event loop from processing user input and updating the interface.

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

To prevent a while loop from blocking the event loop, you can use asynchronous programming techniques such as multithreading or non-blocking I/O operations. This allows the event loop to continue processing user input and updating the GUI while the while loop is running in the background.

3. What are the consequences of a while loop blocking the event loop in a Windows GUI program?

When a while loop blocks the event loop in a Windows GUI program, the user interface becomes unresponsive and may appear frozen. This can lead to a poor user experience and may cause the program to become unmanageable.

4. Are there any best practices for managing while loops in a Windows GUI program written in C++?

One best practice for managing while loops in a Windows GUI program is to avoid long-running operations within the loop that could block the event loop. Instead, break up the operation into smaller tasks and use asynchronous programming techniques to run them in the background.

5. How can I debug and troubleshoot issues related to a while loop blocking the event loop in my C++ Windows GUI program?

To debug and troubleshoot issues related to a while loop blocking the event loop, you can use debugging tools such as breakpoints, step-through debugging, and profiling tools to identify the specific code that is causing the blockage. Additionally, logging and error handling can help pinpoint the root cause of the issue.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
424
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Replies
6
Views
979
  • Engineering and Comp Sci Homework Help
Replies
2
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top