PrimalForms 2009: New Packager STA Mode

Last month’s service build included a new addition to PrimalForms 2009’s packager: STA Mode (Single Thread Apartment), which allows you to perform operations that may have previously failed, such as accessing Window’s Clipboard.

Some of you may have attempted to use the Clipboard in your forms only to encounter issues within PowerShell.

For example, if you try to copy data from a DataGrid you will get the following error:

Copy From Grid

This issue also comes up if you try to access the Clipboard directly from within Powershell:

[System.Windows.Forms.Clipboard]::SetText("Paste this text to the clipboard")

Clipboard Error

These issues arise because PowerShell runs in a MTA mode (Multi-Thread Apartment), yet the clipboard operations require the process to be in STA mode (Single Thread Apartment). A full explanation of Thread Apartments goes beyond the purpose of this blog, so we will provide a very condensed explanation: Thread Apartments boil down to how a process manages legacy COM objects and their communications between multiple threads.

The new PrimalForms 2009 Packager’s STA Mode allows you to package scripts that can access STA dependent operations, such as the Clipboard. To create STA packages, you simply check the “Run In STA Mode” options in the General Packager Settings page and press the Build button.

Packager Settings

Now your packaged scripts can access the clipboard without encountering the issues mentioned above.

Please note that STA Mode is only compatible with WIN32 Windows and WIN64 Windows packager settings.