Skip to main content
Inspiring
September 25, 2017
Answered

Check if external progam is installed - LrTasks.startAsyncTask wait for answer

  • September 25, 2017
  • 1 reply
  • 592 views

In one of my plug-ins I need to check whether an external tool is installed.

For this I added in the Plug-in manager a section called "Configuration" with a button.
However I don't want to bother my clients with searching for the executable on the file system.

So I want to execute with a command to check whether it is installed.

<pre><code>LrTasks.execute( command )</code></pre>

Since the code is added to PluginInfoProvider I can't use LrTasks.execute directly, because then I get the error:

We can only wait from within a task.

So I embedded it within a LrTasks.startAsyncTask ( function()

...

end)

How can I wait for the async task to finish and get the results?

This topic has been closed for replies.
Correct answer johnrellis

When the async task completes, it could set bound values in a properties table that control UI elements in the custom manager section. E.g. there could be a static_text() that initially shows "Searching for external tool", and when the search finishes, the task sets it to "Found external tool" or "You need to install external tool".  You could control the visibility of other elements, etc. as well.

But in general, if you want one async task to wait for a condition (e.g. the completion of another async task), you need to use a spin loop:

while not completed do LrTasks.sleep (0.25) end

While this offends the more delicate architectural sensibilities, practically it works very well and consumes very little CPU, as long as you program it carefully.  A number of my plugins are forced to do this for various reasons. 

1 reply

johnrellis
johnrellisCorrect answer
Legend
September 25, 2017

When the async task completes, it could set bound values in a properties table that control UI elements in the custom manager section. E.g. there could be a static_text() that initially shows "Searching for external tool", and when the search finishes, the task sets it to "Found external tool" or "You need to install external tool".  You could control the visibility of other elements, etc. as well.

But in general, if you want one async task to wait for a condition (e.g. the completion of another async task), you need to use a spin loop:

while not completed do LrTasks.sleep (0.25) end

While this offends the more delicate architectural sensibilities, practically it works very well and consumes very little CPU, as long as you program it carefully.  A number of my plugins are forced to do this for various reasons. 

Inspiring
September 27, 2017

Hi John,

Thank you. You set me in the right direction. Using the property table solved the problem I was facing. Looked in the wrong direction.