locked
In a VSIX, can we assume single thread apartment (STA) mode (VS 2015, 2013, 2012, and 2010) when using Clipboard methods? RRS feed

  • Question

  • I am working on a VSIX extension for VS 2015, and will later backport it to VS 2013, 2012, and 2010. The extension does a lot of interaction with the clipboard, so it uses several methods in the System.Windows.Forms.Clipboard class. As stated in the class's documentation, the Clipboard class can only be used in threads set to single thread apartment (STA) mode.

    I know that in 2008 and earlier, Clipboard methods had to be invoked in a separate thread set to STA mode. But in VS 2015, I am finding that:

    (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)

    is always true. However, this is just circumstantial evidence. 

    If it is always true for extensions in these versions, it would cut down on the complexity of the code that has to use the Clipboard class. But if I can't count on it, I need to spin off an STA mode thread to do the work.

    Assuming I don't explicitly create new threads that are not set to STA mode, is this going to be true for VS 2015, 2013, 2012, and 2010?

    Thanks.

    Sunday, June 26, 2016 11:28 PM

All replies

  • Hi kengr,

    >>"is this going to be true for VS 2015, 2013, 2012, and 2010?"

    Thank you for your post. I have not found any documents related to this. I suggest you create a common method which could detection apartment state for you.

    public void SetData(string format, object data)
    {
        if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
        {
            System.Windows.Forms.Clipboard.SetData(format, data);
        }
        else
        {
            Thread t = new Thread(()=> {
                System.Windows.Forms.Clipboard.SetData(format, data);
            });
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 27, 2016 9:07 AM
  • Thanks for your reply.

    Yes, I do know how to get the code to execute in another thread in STA mode, and I would need to implement methods for several of the Clipboard operations.

    Knowing how to do it is not the question.  The question is, in a VSIX, do I need to do it?  I'm trying to keep the code in this extension as lean and simple as possible, and experimentation seems to show that I don't need to make these calls in a separate STA mode thread, in this environment.

    You state that "I have not found any documents related to this," and neither did I, which is why I asked the question.

    To be completely safe, I will just assume for now that the current thread might not be in STA mode, and I will include the extra code to execute these methods in a separate STA mode thread if the current thread already isn't STA.

    If I find a definitive answer, I will post it to this thread.

    Monday, June 27, 2016 2:38 PM