AI Coding in Practice: Building a Desktop Tool Without UI Freezes

    This article examines why AI-generated code that compiles is not necessarily usable, using a generic batch audio-video extraction desktop tool as the case study. It shows how to turn vague expectations into a verifiable requirements checklist, why keeping the UI responsive separates engineering code from toy code, and how a producer-consumer pattern with background workers, queue-based messaging, and timer-based UI updates prevents freezes. It also covers robustness details such as dependency pre-checks, overwrite protection, atomic writes, cancellation, and path handling, plus a practical acceptance checklist and the role of follow-up prompts in improving generated results.

    1. Introduction: Why “It Runs” Is Not the Same as “It Works”

    As intelligent coding tools spread quickly, getting compilable code from a single natural-language request is no longer impressive. The real difficulty is the engineering usability of the generated result: Is the structure clear? Are exceptions handled? Do long-running tasks block the UI thread? This article uses a generic “batch audio-video extraction desktop tool” as a case study and abstracts a transferable set of practices from it. It does not target any specific product; it discusses the technology itself.

    Three core points are worth extracting from the original scenario. First, the completeness of the requirements determines the quality of the generated code. Second, “no UI freezes” is the dividing line between toy code and engineering code. Third, after generation, another round of follow-up questions and manual acceptance is still required.

    2. Requirements: Turning Vague Expectations into a Verifiable Checklist

    Most failures are not caused by limited model capability but by requirements that are too general. It helps to break the requirements into four categories, each verifiable item by item:

    • Functional: Support common video inputs and export mainstream audio formats; provide an input list, output format, and directory settings.
    • Interaction: Complete the action loop for import, start conversion, and exit; clear layout sections; scrollable long lists.
    • Feedback: Progress bar, status messages, and a log area; readable reasons for failed items.
    • Non-functional: The UI stays responsive during conversion; repeated runs do not overwrite existing output; missing dependencies are caught in advance.

    Writing “no freezes” as an explicit constraint is the most valuable sentence in the prompt, because it directly forces the implementation to introduce concurrency.

    3. Architecture: The Main Thread Only Schedules; Heavy Work Goes to the Background

    The event loop of a desktop UI framework is single-threaded. Any blocking call executed on the main thread will push the window into a “not responding” state. The correct shape is a producer-consumer model:

    1. import threading, queue
    2. task_q: queue.Queue = queue.Queue()
    3. msg_q: queue.Queue = queue.Queue()
    4. def worker():
    5. while True:
    6. item = task_q.get()
    7. if item is None:
    8. break
    9. try:
    10. # Call the external transcoding capability; the time-consuming work runs here
    11. msg_q.put(("progress", item.name, 0.5))
    12. except Exception as e:
    13. msg_q.put(("error", item.name, str(e)))
    14. finally:
    15. task_q.task_done()
    16. threading.Thread(target=worker, daemon=True).start()

    On the UI side, a timer polls the message queue and writes state back to the widgets, avoiding direct cross-thread UI manipulation:

    1. def poll():
    2. while not msg_q.empty():
    3. kind, name, payload = msg_q.get_nowait()
    4. # Update the progress bar, status bar, or log
    5. root.after(100, poll)

    This three-part pattern of “background computation + queue communication + timer-based write-back” is a general approach to keeping the UI smooth, independent of any specific framework.

    4. Robustness: Details That Are Easy to Overlook

    The quality of generated code often shows up in additions that were never explicitly requested:

    1. Dependency pre-check: At startup, verify whether the external transcoding component is available; if it is missing, show installation guidance instead of failing midway through a run.
    2. Overwrite protection: When an output file has the same name, automatically append a sequence number or timestamp to protect existing data.
    3. Recursive directory scanning and format coverage: Support scanning subdirectories, and cover common containers and encodings in the input and output format lists.
    4. Atomic writes: Write to a temporary file first, then rename it, so an interruption does not leave a partial file.
    5. Cancellation and interruption: Provide a stop entry point for long tasks, and let the background thread sense the exit signal.
    6. Encoding and paths: Handle Chinese characters, spaces, and overly long paths compatibly.

    5. Verification: An Executable Acceptance Checklist

    • Static checks and syntax validation pass on the first try, with no undefined references.
    • Cold start time is within an acceptable range, and the first screen has no blank blocking.
    • Dragging the window and switching tabs stay smooth while a conversion is running.
    • Deliberately create failure samples (corrupted files, directories without permission); error messages are readable and the program does not crash.
    • Two consecutive runs do not overwrite each other’s output.
    • When the window is closed, background tasks are reclaimed correctly, with no leftover processes.

    6. Collaboration: Treating Follow-Up Questions as an Iteration Tool

    Effective follow-up questions after generation improve the final result more than the first round of prompting. For example, asking about the default output directory, whether to preserve directory hierarchy, and the retry strategy for failures can expose implicit assumptions in the implementation. Checking the answers against the code instead of trusting them wholesale is a basic engineering attitude.

    7. Conclusion

    The value of intelligent coding is not finishing the last line for you, but moving verifiable engineering constraints to the front. Writing concurrency, feedback, exception handling, and overwrite protection into the requirements, then pairing them with a checklist-based acceptance process, reliably yields a desktop tool that both runs and works well. The same method applies to data processing, batch scheduling, and similar scenarios, with clear transferable value.