I’ve created a separated thread to load the content (textures, songs, etc) and it works well but now I need to set some text in the clipboard using the following statement
Clipboard.SetText("test");
from that separated thread and of course I get this exception
An exception of type ‘System.Threading.ThreadStateException’ occurred in System.Windows.Forms.dll but was not handled in user code
Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
The problem here is that I don’t know how to access to the main thread to do an Invoke() or BeginInvoke() in the main thread.
And you call SetText from your thread.
Be warned this does not check if the control has a Text property. You can directly replace “c” with the control object if you prefer (and update the method and delegate accordingly)
I dunno if this should work to set text to the clipboard, if the clipboard is an OLE element.
I don’t know which one I’m using. I just created a new game using “Monogame Windows Project” (DirectX) template in Visual Studio.
To be able to use Clipboard.SetText(string) I’m using System.Windows.Form namespace but I don’t have any Form in the game. It’s a DX full screen game.
In your example you’re using Invoke with a control but like I said before I don’t have any control nor any form in the game so would you please show me an example using SetClipboard?
I’m trying to pass some text to the clipboard from a thread different than the game’s thread. That’s why I’m asking how to get the game’s thread.
I need to set the text “Test” to the clipboard. The way I know to do that is using the statement SetClipboard(“Test”) but if it must be done using some workaround it’s OK.
The COM threading model is called an “apartment” model, where the execution context of initialized COM objects is associated with either a single thread (Single Thread Apartment) or many threads (Multi Thread Apartment). In this model, a COM object, once initialized in an apartment, is part of that apartment for the duration of it’s runtime.
The STA model is used for COM objects that are not thread safe. That means they do not handle their own synchronization. A common use of this is a UI component. So if another thread needs to interact with the object (such as pushing a button in a form) then the message is marshalled onto the STA thread. The windows forms message pumping system is an example of this.
If the COM object can handle its own synchronization then the MTA model can be used where multiple threads are allowed to interact with the object without marshalled calls.
Glad I could help
Note that threads created with the Thread class are somewhat obsolete.
Give a preference to Task when possible. Or any of the new threading possibilities with C# 4.x (async, await…)