Running a Cocoa GUI in a non-main thread

前端 未结 3 1350
谎友^
谎友^ 2020-12-19 05:13

I am having a gui/threading related problem in developing a cocoa user interface. The application is designed like this:

Main Thread (#1): parses arguments, loads pl

相关标签:
3条回答
  • 2020-12-19 05:34

    Don't. This approach will never work. Even if you fix your current problem (the window not drawing) you'll immediately run into another obscure, impossible-to-fix problem, and another, and another. Cocoa expects the GUI thread to be the main thread, end of story.

    0 讨论(0)
  • 2020-12-19 05:36

    Do all in the background thread except updating the GUI. I see that you have only a line where you need to update the GUI. So do it the way you're doing it, except that you execute all GUI updates in the main thread:

    dispatch_async(dispatch_get_main_queue(), ^
    {
        [self updateWindows];
    });
    

    Now I don't know what's updateWindows, I assumed that this wouldn't create a race condition.

    0 讨论(0)
  • 2020-12-19 05:53

    Why not reverse the problem? Have the main thread spawn a thread (let's call this the app thread), then block before spawning the GUI. The app thread will parse arguments, load plugins, etc. After it's initialization is done, the app thread will signal the main thread to go ahead and launch the GUI.

    0 讨论(0)
提交回复
热议问题