0

He, I programmatically create a window in a thread when a button in my main window is clicked. If this button is clicked again, the window closes. Fine. But when the user closes the window via its close button, the thread isn't aware of that, running on. And if the user clicks on the button in the main window, the thread is trying to close the window and the app is crashing. So I need to check within the thread if the window exists. How to do that? Thanks

-(void)prefsWindow:(id)sender {
    NSRect frame = NSMakeRect(200, 200, 640, 480);
    NSWindow *pwindow  = [[NSWindow alloc] initWithContentRect:frame
                                                     styleMask:NSTitledWindowMask 
                                                       backing:NSBackingStoreBuffered
                                                         defer:NO];
    [pwindow center];
    [pwindow makeKeyAndOrderFront:NSApp];
    while (prefsA==1) {

        usleep(250000);}
    [pwindow close];
    [NSThread exit];
}
fw2601
  • 83
  • 6

1 Answers1

4

Many things wrong with this code.

First, don't create windows from a thread. Unless NSWindow is explicitly documented as thread safe, you can't use it from a thread.

Secondly, you should never poll with sleep().

Finally, there is no reason to create a thread for a window like this.

bbum
  • 162,346
  • 23
  • 271
  • 359