Run in Background
Running Fast Video Cataloger scripts in the background
Running scripts in the background¶
Normally, when you run a script in FVC, it runs on the UI thread. This allows you to create your own user interface if needed. FVC will run your script and be paused until it has finished running.
However, if you want to run a longer script you can run part of the script in a worker thread. The FVC scripts are started with async support so if you run your work in a worker thread and await correctly, FVC will be responsive when your script is running. Be aware though that what the user does in the user interface might interfere with your script, the user could for example remove a video or change selection.
At the top of your script, we need to bring in the C# task library:
using System.Threading.Tasks;
Then to run a function in your script, you write this:
await Task.Run(()=> MyFunction());
Task.Run(…)will start a task, and MyFunction will run in the task. The await causes the script to wait until the task has finished runningMyFunction().
The "Run on worker thread" checkbox¶
A script run as a custom action has a second way to get off the user interface thread: the Run on worker thread checkbox on the script page of the Custom Action Wizard.
It is a blunter instrument than the code above. Ticking it hands the whole script to a background thread, so the program stays responsive, but nothing is marshalled back for you and the script cannot show a user interface of its own - no message box, no window, no dialog. A script that fails there fails quietly.
Unticked, the script runs on the user interface thread exactly as it does from the Script Console, so it can show whatever it likes while the program waits for it.
Prefer the await Task.Run(...) above when the script needs both: it keeps the program responsive and leaves the parts that talk to the user on the thread that can.