Re: accessing newly-created files
Re: accessing newly-created files
- Subject: Re: accessing newly-created files
- From: "Louis C. Sacha" <email@hidden>
- Date: Thu, 15 Apr 2004 18:33:37 -0700
Hello...
Brian Bergstrand wrote:
A thread is really overkill for this situation.
I would say the same thing about using knote and kevent() to do it :)
It's probably just that we are looking at the problem from two
different backgrounds... If using knote and kevent() is something
that is simpler and more familiar to you, then that might be the best
way to go.
In terms of avoiding the beachball, I dont think it matters what
method or function you are using that causes the thread/process to
sleep (or for that matter, any code that requires a large amount of
time to run)-- it's based on the total amount of time it has been
since the last time the application's event loop went around. I'm not
sure of the exact amount of time you can hold up the event loop
before it appears, but as long as we're talking about a total of less
than a second it should be okay.
Also, I wasn't advocating creating a new thread -- and I agree that
would probably be overkill in this situation -- but instead just
putting the main thread that already exists to sleep.
Using the NSThread method sleepUntilDate: on the main thread is
probably equivalent to using any of the variations of sleep(),
although based on the man page for usleep() it appears that it would
cause the whole application to stop processing, where using NSThread
sleepUntilDate: would only stop execution of a specific thread in a
multithreaded application. That difference probably isn't an issue in
this case. You can use the sleepUntilDate: method to sleep the main
thread for as small of an amount of time as you would like (the
NSTimeInterval used by NSDate refers to seconds, and is equivalent to
a double, so just use whatever decimal fraction of a second you need).
The other issue is whether checking for the existence of the file is
enough? Even if the knote tells you that the file was created, unless
this only happens after the file fully written, you might still end
up with the same problem that "the end of file had not been reached"
unless you have some other way of checking that the file has been
completely written.
If NSImage is unable to load the image, the initWithContentsOfFile:
method should simply free the allocated NSImage instance and return
nil (which would just cause the loop to repeat until it suceeded in
loading the image). Unfortunately based on your comments about
needing try&catch it seems that an exception is being raised
somewhere, which would break the code I suggested. ((in real life,
there would also need to be some sort of error checking added in case
the written file is finished writing but is invalid, to avoid being
caught in the loop permanently. For example, incrementing an integer
each time the loop and checking to see if it has passed some
threshold value where you know the image should have been completed
by that point.))
There is a Cocoa conceptual article on exception handling in the on
disk developer documentation (and somewhere on the web at
developer.apple.com as well):
/Developer/Documentation/Cocoa/Conceptual/ExceptionHandling/index.html
Hope that helps,
Louis
Hmmm... spinning ball of doom is bad. I saw the sleepUntilDate
method, but I decided to wait to try it until I knew more about it.
I'd like to avoid the beach ball if at all possible. I haven't used
try {} catch {} blocks with Objective-C yet. I don't see any of that
in the two Cocoa/Objective-C books that I have. Is there something
similar where I can catch the file I/O error and then continue once
it works?
David
On Apr 15, 2004, at 2:08 PM, Louis C. Sacha wrote:
Hello...
Perhaps something like the following would work?
NSImage *theImage = nil;
while (!theImage)
{
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
theImage = [[NSImage alloc]
initWithContentsOfFile:@"/the/path/to/the/file.img"];
}
Note that if you use the sleepUntilDate: method on the main thread
continuously (or with a long time interval) you will get the
spinning beachball cursor.
_______________________________________________
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.