Speed and (sheets or panels)

I currently have a real problem with speed, and it seems to be thread related.

I read somewhere that the AppleScript code that is running within the context of a panel is in a seperate thread to the main thread. My experience indicates that this is true and that the panel thread is a lower priority thread and this is mostly what I want. But I have managed to code myself into a corner in one circumstance where this behaviour is getting in my way and I don’t know how to get out of it.

I am using the open panel dialog as follows


	set thisPanel to open panel
	tell thisPanel
		set can choose directories to true
		set can choose files to true
		set allows multiple selection to true
		set name to "openpanel"
	end tell
	display panel thisPanel attached to window "main"
	return

The function returns immediately with the panel displayed. I then have the on panel ended handler


on panel ended thePanel with result theResult
	set the pathNames to (path names of open panel as list)
	AddItemsToTable(pathNames)
end on panel ended

Now because the on panel ended function is still running in the panel thread, and the panel is still displayed, AddItemsToTable which has a lot of work to do is running in the low priority thread, and is snail like slow. So I tried adding the files to a global list, and then using the on idle handler to check the length of the list (I am using it for something else as well) before doing the work. Unfortunately the on idle handler also runs in its own low priority thread, so that doesn’t help.

I then thought of just using the standard additions choose folder or choose file commands because they would just block the main thread until they return. But unfortunately neither of them appear to allow you to select both files and folders.

Has anybody got suggestions as to how to get around this.

Kevin

Untested:

on panel ended thePanel with result theResult
   close panel open panel
   set the pathNames to (path names of open panel as list)
   AddItemsToTable(pathNames) 
end on panel ended

Thanks for the suggestion jj.

I have tried it out without improvement. I was a bit nervous at doing so because the on panel ended handler is normally called as a result of calling close panel. But the open-panel object behaves slightly differently.

It didn’t cause the recursive overload that I was expecting but neither did it speed up the code.

Kevin

I think that “on panel ended” is called when any panel is closed due to user interaction, when the panel is the only “interactuable” element in your app, typically when you display a panel attached to a window.
Maybe the slow-behaviour you describe is due to the work with the table itself, since working with tables in a AS-Studio app is an incredible slow task, specially when you must fill several columns…

I chomped out a bit too much when putting together the original post.

The reason I had set name to “openpanel” in the tell open panel block was so that I needed to check for the name of the panel in the on panel ended handler, because I have about 5 different panels that can come up.

In two of those cases the panel is just displaying a progress bar and cancel button and is closed when the cancel button is clicked and the clicked handler then sets a global flag, the main thread calls close panel when it has finised its work or the global flag has been set. It is definitely the close panel handler that results in the on panel ended handler being called.

As for speed, if I hard wire a few paths into the AddItemsToTable method and have it being called in the main thread, it is about 5 times faster. So I know this is an issue with the panel threads.

Kevin