Re: Thread and Return key strangeness
Re: Thread and Return key strangeness
- Subject: Re: Thread and Return key strangeness
- From: email@hidden
- Date: Tue, 07 May 2002 09:57:52 -0700
Lorenzo Puleo wrote:
|Hi,
|I have got a strange trouble launching a NSThread after I pressed the Return
|key. I would like to understand what I wrong.
|
| ...
|
|The "Start" button launch this routine:
|[NSThread detachNewThreadSelector:@selector(StartTask) toTarget:self
|withObject:nil];
|
|And the StartTask does that:
|- (void)StartTask
|{
| NSAutoreleasePool *pool = [NSAutoreleasePool new];
|
| [consChooseFolderBtn setEnabled:NO];
|
| // ... some simple code here (copy files...)
|
| [consChooseFolderBtn setEnabled:YES];
|
| [pool release];
| [NSThread exit];
|}
|
|Also, if I launch the StartTask routine without thread this way
|[self StartTask]
|it always runs properly, either if I went back to the console clicking the
|"OK" button with the mouse and pressing the Return key.
The key: you're trying to manipulate the UI in a non-UI thread. This isn't guaranteed to work, because the UI thread is *also* manipulating the UI thread. If you're lucky, neither thread steps on the other. Clearly, you're not being lucky. The cure? Disable the button in the main thread, before you call detachNewThread. When the thread is done, post an ApplicationDefined event to the event queue (which can be done safely from any thread). The main thread should intercept the event and reenable the button. (See NSApplication and NSEvent for more on event handling, plus the "Event Handling" topics in the documentation.)
Everything works if you run StartTask directly because it's then being run in the main (UI) thread, so no conflicts can occur.
I'm surprised that "StartTask" is even being called. DetachNewThreadSelector is documented as expecting the selector to take a single argument (the object passed to "withObject:"), which implies that your method should be "- (void) StartTask: (id) junk".
Glen Fisher
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.