开发者

Continuos UI update Mac Application?

开发者 https://www.devze.com 2023-03-18 20:01 出处:网络
I want my Mac cocoa application to continuously update the View even if it goes to back ground? Something like Activity Monitor or DigitalColor Meter.

I want my Mac cocoa application to continuously update the View even if it goes to back ground? Something like Activity Monitor or DigitalColor Meter.

How can I achieve such behavior?

Thank you!

EDIT: I get context from NSGraphicContext and then create a thread to do the job in back ground.

- (void)drawRect:(NSRect)dirtyRect
{   
    context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    [NSThread detachNewThreadSelector: @开发者_高级运维selector(ExecuteDrawingInBackGround:) toTarget: self withObject: nil];
}


You can't use the Mac UI API from a background thread because it's not thread safe.

Programs that redraw themselves continuously do so by having the background threads (if any) notify the main thread that something has changed and needs redrawing. This is easiest done by having the background thread send -performSelector:OnMainThreadWithObject:waitUntilDone: to some object, usually the document or a view controller.

Alternatively, you can also use NSTimer to fire regularly and avoid background threads altogether.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号