Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't Find Cocoa GUI Thread in C++ Application w/ Objective-C++ GUI
    text
    copied!<p>I have a multi-posix-threaded Linux C++ application without a GUI that I want to be able to use the occasional Cocoa control in, namely the file upload/download dialogs and the alert.</p> <p>I'm far from an expert with Cocoa, but was able to build a few Objective-C++ test/demo apps that worked as intended.</p> <p>Now that I've integrated the Cocoa code into my application, it seems that I'm having trouble posting things to the main GUI thread. Maybe I didn't do what I needed to do to create one, I'm really not sure. Here's what I have in my .mm file:</p> <pre><code>#ifdef MACOS @interface CocoaInterface : NSObject { } - (id) init; - (void) ShowFileUploadDialog; - (void) ShowFileDownloadDialog; @end @implementation CocoaInterface - (id) init { cout &lt;&lt; "Creating NSAutoreleasePool" &lt;&lt; endl; NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; cout &lt;&lt; "Creating NSApplication" &lt;&lt; endl; NSApplication* app = [[NSApplication alloc] init]; cout &lt;&lt; "Calling NSApplication::finishLaunching" &lt;&lt; endl; [app finishLaunching]; [super init]; return self; } - (void) ShowFileUploadDialog { cout &lt;&lt; "Entering ShowFileUploadDialog" &lt;&lt; endl; if ([NSThread isMainThread]) { // Show file dialog cout &lt;&lt; "Calling NSRunAlertPanel" &lt;&lt; endl; NSRunAlertPanel(@"This is a test", @"Does it work?", @"Yes", @"No", @""); } else { //NSRunAlertPanel(@"This is a test", @"Does it work?", @"Yes", @"No", @""); cout &lt;&lt; "Redirecting ShowFileUploadDialog call to main thread." &lt;&lt; endl; [self performSelectorOnMainThread:@selector(ShowFileUploadDialog) withObject:nil waitUntilDone:YES]; } } - (void) ShowFileDownloadDialog { cout &lt;&lt; "Entering ShowFileDownloadDialog" &lt;&lt; endl; if ([NSThread isMainThread]) { // Show file dialog cout &lt;&lt; "Calling NSRunAlertPanel" &lt;&lt; endl; NSRunAlertPanel(@"This is a test", @"Does it work?", @"Yes", @"No", @""); } else { //NSRunAlertPanel(@"This is a test", @"Does it work?", @"Yes", @"No", @""); cout &lt;&lt; "Redirecting ShowFileDownloadDialog call to main thread." &lt;&lt; endl; [self performSelectorOnMainThread:@selector(ShowFileDownloadDialog) withObject:nil waitUntilDone:YES]; } } @end #endif </code></pre> <p>I call this from the code I have in the various threads I have processing incoming network messages:</p> <pre><code>cout &lt;&lt; "Creating CocoaInterface." &lt;&lt; endl; CocoaInterface* interface = [[CocoaInterface alloc] init]; cout &lt;&lt; "Calling CocoaInterface::ShowFileDownloadDialog." &lt;&lt; endl; [interface ShowFileDownloadDialog]; </code></pre> <p>This hangs on trying to perform the selector -- as if it can never actually find the main thread. A backtrace in GDB shows me waiting on a semaphore forever.</p> <p>When I uncomment the NSRunAlertPanel call before the performSelectorOnMainThread call, I get a white block in the shape of the dialog, but it doesn't fully draw or process any messages, presumably because it's not on the main GUI thread.</p> <p>It seems that I don't have a proper GUI thread or just can't get to it from where I am. I suspect that I've missed something in initialization. Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload