Re: Thread and Return key strangeness
Re: Thread and Return key strangeness
- Subject: Re: Thread and Return key strangeness
- From: Lorenzo Puleo <email@hidden>
- Date: Tue, 07 May 2002 22:47:09 +0200
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
Hi,
thank you for your reply. I would like to do some question more, if I don't
waste your time, please.
I image APIs like
session = [NSApp beginModalSessionForWindow:previewWindow];
or showing an incremental "Items to do" value like
[consTaskComment setObjectValue:stringItemsToDo];
or showing a progressBar increasing its value
could block the Thread too;
If so, I would be really surprised.
Since after the call detachNewThreadSelector
all the following lines have been executed immediately, how could I solve my
trouble?
I have to do:
Disable all the buttons
{
Start a loop which scans inside a long NSArray.
If an error occurred I have to show a modal dialog asking for continuing or
stopping. The modal dialog is complex. There are a lot of variable objects.
At each item scanned I have to increment a NSText field and a progress bar.
The use should be able to stop the task clicking on the "Stop" button.
}
Enable all the buttons
I have done that very well without threads, but I was not able to stop the
task clicking on the "Stop" button. So I introduced the threads, ...and the
trouble above.
Please, any assistance/sample-code is appreciated. Thanks.
Please note that the things are much much complex than I am describing here.
--
Lorenzo Puleo
mailto:email@hidden
_______________________________________________
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.