I am updating a CodedUI test program. Part of the update I have been working on is threading the test so that I can cancel the test, when necessary, without needing to kill the test program. The Task I create works fine until the CodedUI test copies data to the Clipboard then tries to retrieve that so it can dump it to a database. Without the task, this works with no issues, but with the task, Clipboard.GetData(DataFormats.Text) returns null.
Because Keyboard.SendKeys is used before the Clipboard request, I tried CodedUI Test: Keyboard.SendKeys not working to see if that would help, but it did not.
Getting data off the Clipboard inside a BackgroundWorker solved the issue. Though nothing threw errors relating to the Thread Apartment State, setting it to STA makes things work.