Re: NSThread Question
Re: NSThread Question
- Subject: Re: NSThread Question
- From: "John W. Whitworth" <email@hidden>
- Date: Thu, 9 May 2002 11:29:15 +0100
The problem is that the GUI is not responding to events while you are
processing items in the main run loop (thread). So I don't think you can
avoid using a thread to process items. Use Distributed Objects for
communication, it's not difficult. I think you could put the main loop
into a modal state where the GUI is updated with progress of the thread
and the user is prevented from doing anything but clicking the stop
button to abort the thread. Perhaps someone with more experience than I
could give a definitive answer as to how best to do this, so I'm posting
this message to Cocoa Dev as well as replying to you personally.
John Whitworth
On Wednesday, May 8, 2002, at 03:44 PM, Lorenzo Puleo wrote:
on 4/24/02 1:46 AM, John W. Whitworth at email@hidden wrote:
Once you start using threads, issues of thread safety are raised. Any
visual interface updates should be done from the main run loop. A
thread
should call a selector in the main run loop via distributed objects.
Also be aware of data integrity issues if more than one thread is
reading and writing to the same object.
John Whitworth
Thank you for your help. The example code you suggested won't compile
with
my PB 1.1.1. Anyway I have found a line of that sample very useful,
so I
made this (it works very fine! - If you think some error could occur
with
this code in the future, please advise):
- (IBAction)executeLoop
{
if(isRunning == YES){
// isRunning is a variable instance of this class
isRunning = NO;
return;
}
isRunning = YES;
[NSThread detachNewThreadSelector:@selector(processTheItems)
toTarget:self withObject:nil];
}
- (IBAction)processTheItems
{
int i;
// gItemsToDoArray is a variable instance of this class
for(i = 0; i<[gItemsToDoArray count]; i++){
if(isRunning == NO) break;
[self processSingleItem:[gItemsToDoArray objectAtIndex:i]];
}
// This shows an alert telling how much items
// were been processed
[self showReport:i];
}
Thanks Drew!
--
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.
Hi John,
unfortunately during the long task I have to set some UI objects
(progress
bar, counters in NSTextField, diplaying modal dialogs if an error
occurred,
etc, etc. So I can't use threads so easily. They make conflicts with
the UI.
So I tried to do this:
[self performSelector:@selector(ExecuteLongTask) withObject:nil
afterDelay:0.0];
This releases the button properly (the button becomes white again
immediately).
But if I try to click again the button to Stop the task, I can't.
I guess I have to let the main loop detect this mouse click, so I could
be
able to set a global variable which breaks the long task, but I don't
know
how to do this with Cocoa. I used to do that in an old Carbon
application,
and it worked fine.
Please could you help me?
I would prefer to avoid threads, because they don't let me update the UI
during the long task so easily, and up above because when the long task
is
running the user doesn't need to (and he must not) do anything but stop
it.
Thanks and Regards.
--
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.