Processing中setup与draw是否并行?fileselected执行时机及draw等待方法问询
Let's tackle your Processing questions one by one, since they all tie into how the framework handles execution flow and events:
1. Do setup() and draw() run in parallel?
Nope, they don't—Processing uses a strictly serial execution model. Here's the clear order of operations:
- When your program launches, it runs the
setup()function exactly once, and it won't move on until every line of code insetup()finishes executing. - Only after
setup()is completely done does it start thedraw()loop:draw()repeats over and over (default 60 times per second) until you close the program. - There's no parallelism here—one finishes before the other starts.
2. Will fileSelected() finish before a while loop runs?
Almost certainly not, unless you've structured your code in a very unusual way. Here's why:fileSelected() is an event callback—it only triggers after the user completes a file selection (usually started by calling selectInput()). Processing doesn't pause its main execution flow to wait for this user interaction.
For example: If you call selectInput() in setup() and immediately follow it with a while loop, that while loop will start running right away. The fileSelected() function won't execute until the user picks a file (which could be seconds later)—by then, your while loop might have already finished or been running nonstop.
Processing handles event callbacks (like fileSelected() or mousePressed()) in gaps between draw() iterations. They never block the main code or force it to wait.
3. How to make draw() wait until necessary parameters are received?
You don't want to actually "block" draw() (that would freeze your program, since Processing relies on the draw() loop for rendering and event handling). Instead, use a state variable to gate draw()'s logic.
Here's a practical, responsive approach:
- Define a boolean variable (e.g.,
boolean dataReady = false;) to track whether your required parameters are available. - In
draw(), check this variable first. If it'sfalse, render a waiting message (like "Select a file to start!") and exit early withreturn. - When your
fileSelected()callback successfully grabs the needed parameters, setdataReady = trueand store the parameters in a accessible variable.
Example Code:
boolean dataReady = false; File selectedFile; void setup() { size(400, 200); selectInput("Pick a file to process:", "fileSelected"); } void fileSelected(File selection) { if (selection != null) { selectedFile = selection; dataReady = true; // Mark parameters as ready to use } } void draw() { background(255); textAlign(CENTER, CENTER); if (!dataReady) { textSize(16); text("Waiting for file selection...", width/2, height/2); return; // Skip core logic until parameters are ready } // Now run your main draw logic using the selected file textSize(14); text("Selected file: " + selectedFile.getName(), width/2, height/2); // Add your processing code here... }
This keeps your program responsive, gives the user clear feedback, and ensures draw() only runs its core logic once it has all the necessary data.
内容的提问来源于stack exchange,提问作者shiva allani

