How do I update a progress bar in Cocoa during a long running loop?

后端 未结 4 537
悲&欢浪女
悲&欢浪女 2020-12-29 00:17

I\'ve got a while loop, that runs for many seconds and that\'s why I want to update a progress bar (NSProgressIndicator) during that process, but it updates onl

4条回答
  •  Happy的楠姐
    2020-12-29 00:43

    I believe, my loop prevents other things of that application to happen.

    Correct. You need to break this up somehow.

    One way would be a timer, with you whittling away the queue a little at a time in the timer callback. Another would be to wrap the code to handle one item in an NSOperation subclass, and create instances of that class (operations) and put them into an NSOperationQueue.

    Does this have to do with threads or something?

    Not necessarily. NSOperations run on threads, but the NSOperationQueue will handle spawning the thread for you. A timer is a single-threaded solution: Every timer runs on the thread you schedule it on. That can be an advantage or a disadvantage—you decide.

    See the threads section of my intro to Cocoa for more details.

提交回复
热议问题