Re: Threading - How its done?
Re: Threading - How its done?
- Subject: Re: Threading - How its done?
- From: Karl von Moller <email@hidden>
- Date: Tue, 6 May 2008 16:07:51 +1000
Hi Michael,
Thanks for that - I removed the Locks in my code. However I am still
getting crashes after I changed my code to this:
-(void)openPDFandCreatePreview:(id)sender
{
//[progressOutlet setUsesThreadedAnimation:YES];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[progressOutlet startAnimation:nil];
NSPDFImageRep *pdfRep;
pdfRep = [NSPDFImageRep imageRepWithContentsOfFile: finalStringOutput];
pdfImage = [[[NSImage alloc] init] autorelease];
[pdfImage addRepresentation: pdfRep];
[imageWellOutlet performSelectorOnMainThread:@selector(setImage:)
withObject:pdfImage waitUntilDone:NO];
//[imageWellOutlet setImage:pdfImage];
pdfDoc = [[[PDFDocument alloc] initWithURL: [NSURL fileURLWithPath:
finalStringOutput]] autorelease];
[pdfOutlet performSelectorOnMainThread:@selector(setMyPDF:)
withObject:pdfDoc waitUntilDone:NO];
//[pdfOutlet setDocument:pdfDoc];
[progressOutlet stopAnimation:nil];
[pool release];
return;
}
- (void)setImage:(id)sender
{
return [imageWellOutlet setImage:pdfImage];
}
-(void)setMyPDF:(id)sender
{
[pdfOutlet setDocument:pdfDoc];
}
While the setImage: method works very well on it's own I still got a
crash after a while! So, I thought possibly the setting of the
pdfOutlet was the cause. Used the same performSelectorOnMainThread
method on the pdfDoc. But still get crashes! The application won't
display the pdf now either for some reason.
I did change my .h file to read like this:
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
@interface AppController : NSObject {
NSMutableArray *resultArray;
NSMutableArray *fileNameArray;
NSString *theIndex;
NSString *result;
NSOpenPanel *theOpenPanel;
NSString *finalStringOutput;
NSImage *pdfImage;
PDFDocument *pdfDoc;
IBOutlet id buttonOutlet;
IBOutlet id pathTextOutlet;
IBOutlet id tableViewOutlet;
IBOutlet id windowOutlet;
IBOutlet id imageWellOutlet;
IBOutlet id fileNameTextOutlet;
IBOutlet id numberOfFileOutlet;
IBOutlet id currentPathOutlet;
IBOutlet id pdfOutlet;
IBOutlet id pdfNameTextLabel;
IBOutlet id searchFieldOutlet;
IBOutlet id currentFileOutlet;
IBOutlet id currentPathOutletLabel;
IBOutlet id progressOutlet;
- (void)openPDFandCreatePreview:(id)sender;
- (void)setImage:(id)sender;
- (void)setMyPDF:(id)sender;
could it be the setting of the PDF view thats the problem here - Is
this a reasonable assumption on my part?
On 06/05/2008, at 3:12 PM, Michael Vannorsdel wrote:
Locks are used as flow gates to keep two threads from interacting
with the same shared data at the same time. For instance if you're
adding objects to an array and at the same time another thread is
doing the same, it will damage the array as neither thread knows
someone else is modifying it at the same time. So you wrap locks
around the code chunks doing the array editing. When one thread is
inside the lock editing the array, other threads will have to wait
at the lock until the holding thread unlocks it, then another thread
waiting outside the lock will be allowed to pass. Since other
threads have to wait on each other, locking hurts the bonuses to
using threads but is sometimes necessary in some places to make sure
shared data is not corrupted. So the goal is to use them sparingly
if you're unable to redesign the code to avoid multiple threads
modifying the same piece of data simultaneously.
Now if two threads are just reading the same piece of data it's ok
for them to do so at the same time, just not ok for one to be
modifying while another might be working with it as well. This is
what was happening with your UI objects, another thread was
modifying them while the main thread was trying to read and draw them.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden