I’m running a form in a second thread. If I do Ctrl-C to copy text on the clipboard, I get an Exception, “Current thread must be set to a single thread apartment (STA) before OLE calls can be made. (Using the clipboard involves OLE apparently).
Putting the [STAThread] with my thread proc, which is the entry point of my second thread does NOT work. What will work?
[STAThread]
private void MyFormThreadproc(object o)
{
form = new MyForm();
Application.Run(form);
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
When you create the thread, call the SetApartmentState() method before you start it. You can’t do this for threadpool threads.
For example:
Thread thread = new Thread(threadAction); thread.SetApartmentState(ApartmentState.STA); thread.Start();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0